开发者

{% url admin:index %} generating wrong urls

开发者 https://www.devze.com 2022-12-15 00:04 出处:网络
My django site is served up with the following in Apache\'s config: WSGIScriptAlias /studio /django/studio/bin/django.wsgi

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号