Using django/mod_wsgi with apache httpd ErrorDocument seems to confuse reverse URL mapping

68 views
Skip to first unread message

Patrick Ethier

unread,
Jun 27, 2016, 9:09:13 AM6/27/16
to Django users
Hi, I'm trying to set up mod_wsgi and Django to handle authentication and I'm getting a weird problem. After much troubleshooting I've distilled it down to the configs below:

/etc/httpd/conf.d/10-django.conf
<VirtualHost *:80>
ServerName site.internal
DocumentRoot /home/user/git/standard-web-site-no-python-stuff/
alias /static/ /home/user/git/project/app/static/
<Directory "/home/user/git/project/app/static">
  Require all granted
</Directory>

<Directory "/home/user/git/project/app">
  <files "wsgi.py">
    Order Deny, Allow
    Allow from all
    Require all granted
  </Files>
</Directory>
WSGIDaemonProcess site.internal user=apache processes=2 threads=10 display-name=%{GROUP} python-path=/home/user/git/project
WSGIProcessGroup site.internal
WSGIScriptAlias /wsgi-app/ /home/user/git/project/app/wsgi.py
<Location "/">
  Order Deny, Allow
  Allow from all
  ErrorDocument 404 "/wsgi-app/"
</Location>
</VirtualHost>

My Django project is set up VERY simply with the following:
/home/user/git/project/app/wsgi.py
...imports...

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()


/home/user/git/project/app/views.py

def main(request):

  return render(request, "appname/template.tmpl")


def testme(request):

  return render(request, "appname/template2.tmpl")


/home/user/git/project/app/urls.py

urlpatterns = [

  url(r'^$', "app.views.main", name="app-main"),

  url(r'^testme/$', "app.views.testme", name="app-testme"),

]


/home/user/git/project/app/templates/app/template.tmpl

...HTML Stuff...

<a href="{% url "app-testme" %}>testme</a>

...More HTML stuff


--------------

The above app works 100% fine when I go to http://site.internal/wsgi-app/. If I hover over the "testme" anchor, I get http://site.internal/wsgi-app/testme/


BUT! If I go to http://site.internal/non-good-html.html and get redirected by the ErrorDocument 404 directive it shows my views.main() page.

When I hover over the "testme" anchor, I get http://site.internal/testme/ (***Notice the missing /wsgi-app/***)


It seems like the app namespace is not preserved when I'm rendering the main() page from an ErrorDocument directive.


I tried adding app_name="wsgi-app" to the urls.py but it doesn't change anything.


I must be missing something simple as this seems like an easy use-case to implement.


Pat

Patrick Ethier

unread,
Jun 27, 2016, 5:01:48 PM6/27/16
to Django users
Quick update, the answer to my question in reading the source code is to set FORCE_SCRIPT_NAME=/wsgi-app/ from the example below. It also seems that the trailing slash in the WSGIScriptAlias is not appropriate, so the directive should be:
WSGIScriptAlias /wsgi-app ...

It also seems, as per this Django issue: https://code.djangoproject.com/ticket/12464#comment:16

A whole bunch of other sites also propose overwriting with request.META['SCRIPT_NAME'] and the request.environ['SCRIPT_NAME'] in wsgy.py or to use Apache to manually set X-SCRIPT_NAME to this value, It seems they all work in different ways, but the most straight-forward seems to be the FORCE_SCRIPT_NAME in settings.py
Reply all
Reply to author
Forward
0 new messages