views:

181

answers:

2

Hi All,

My django site is served up with the following in Apache's config:

WSGIScriptAlias /studio /django/studio/bin/django.wsgi

My urls.py looks like:

urlpatterns += patterns(
    'django.contrib',
    (r'^admin/', include(admin.site.urls)),
    (r'^accounts/login/$', 'auth.views.login'),
    (r'^accounts/logout/$', 'auth.views.logout'),
    )

...and yet:

[<a href="{% url admin:index %}">admin</a>]

...generates a link to /admin rather than /studio/admin. Bizarrely, the urls within the admin interface itself are fine.

I'm using:

Python 2.5.2-3
Django 1.1.1
mod_wsgi  2.5-1~lenny1
apache2 2.2.9-10+lenny6

Can anyone tell me what I'm doing wrong?

cheers,

Chris

A: 

Your Django instance knows nothing about the /studio part of the URL as it is handled in WSGI. You have to manually prepend it either in templates or, better, in urls.py:

in settings.py:

BASE_URL = '/studio'

in urls.py:

r('^%s/admin/' % settings.BASE_URL, include(admin.site.urls)), ...

Then your url reversing will work as expected. You admin links work because once you are in the admin interface all links are relative.

kibitzer
Django does know about '/studio', it is passed in the SCRIPT_NAME WSGI variable. If Django is still not honouring that automatically where required then Django is arguably broken. These problems with Django were supposed to have been fixed in 1.0 release candidates. I would suggest this issue be taken up on Django users mailing list for attention.
Graham Dumpleton
kibitzer: I'm sorry, but certainly in the context of Django 1.1, your answer is just plain wrong.No BASE_URL kludgery is needed, the following workaround works in the meantime:Just above WSGIScriptAlias I added:RewriteRule ^/newstudio$ /newstudio/ [R]I'll post an answer here when I've gotten to the bottom of the problem...
Chris Withers
It is not plain wrong, it works. Yes, it is not perfect, but your RewriteRule has absolutely nothing to do with solving your problem.
kibitzer
Actually, it does, see the answer I'm about to post.
Chris Withers
A: 

This is a bug in Django, see:

http://code.djangoproject.com/ticket/12464

The problem is that because Apache's rewrite engine is on as a result of rewriting I need to do elsewhere in the virtual host, the SCRIPT_URL environment variable is set. But, when a request is made to /project through Apache, PATH_INFO is empty, which results in SCRIPT_NAME being incorrectly set as an empty string.

Until this bug is fixed, inserting the following RewriteRule into the Apache configuration will safely work around the problem by ensuring that PATH_INFO is never empty.

RewriteEngine On 
RewriteRule ^/project$ /project/ [R]
WSGIScriptAlias /project /django/project/bin/django.wsgi
Chris Withers