{% url admin:index %} 生成错误的网址

发布于 2024-08-16 05:18:47 字数 700 浏览 3 评论 0原文

我的 django 站点在 Apache 的配置中提供以下内容:

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

我的 urls.py 看起来像:

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

...但是:

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

...生成到 /admin 而不是 /studio/admin 的链接。 奇怪的是,管理界面本身内的 URL 没有问题。

我正在使用:

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

任何人都可以告诉我我做错了什么吗?

干杯,

克里斯

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/

...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

, 'auth.views.login'), (r'^accounts/logout/

...and yet:


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

I'm using:


Can anyone tell me what I'm doing wrong?

cheers,

Chris

, 'auth.views.logout'), )

...and yet:

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

I'm using:

Can anyone tell me what I'm doing wrong?

cheers,

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2024-08-23 05:18:47

这是 Django 中的一个错误,请参阅:

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

问题是,由于我需要在虚拟主机的其他地方进行重写,因此 Apache 的重写引擎处于打开状态,因此设置了 SCRIPT_URL 环境变量。但是,当通过 Apache 向 /project 发出请求时,PATH_INFO 为空,这会导致 SCRIPT_NAME 被错误地设置为空字符串。

在修复此错误之前,将以下 RewriteRule 插入 Apache 配置中将通过确保 PATH_INFO 永远不为空来安全地解决该问题。

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

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
氛圍 2024-08-23 05:18:47

您的 Django 实例对 URL 的 /studio 部分一无所知,因为它是在 WSGI 中处理的。您必须在模板中手动添加它,或者更好的是在 urls.py:

in settings.py:

BASE_URL = '/studio'

in urls.py: 中

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

添加它,然后您的 url 反转将按预期工作。您的管理链接有效,因为一旦您进入管理界面,所有链接都是相对的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文