Monday, June 27, 2016

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

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


On Monday, June 27, 2016 at 3:09:13 PM UTC+2, Patrick Ethier wrote:
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7b47b3c3-86b2-41c7-9f27-2291df2ba6ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment