将 Django root 重定向到管理面板

发布于 2025-01-06 03:06:50 字数 1692 浏览 1 评论 0原文

我正在尝试将 Django 站点根目录重定向到 Django 管理面板,并且我基本上已经启动并运行了它。但是,我注意到,除非项目 URL 中有尾部斜杠,否则页面上的链接不包含项目文件夹,而是尝试在站点根目录中查找应用程序。

因此,假设我有一个项目 foo 和一个应用程序 bar。如果您访问 http://server/foo/ (带有尾部斜杠),则一切正常,页面上的链接将转到 http://server/foo/bar< /代码>。但是,如果访问 http://server/foo,生成的链接将转至 http://server/bar,从而生成 404 错误。

如果我将 WSGIScriptAlias 设置为指向 /foo/ 而不是 /foo,那么当我导航到 时,将会出现 404 错误/foo.我尝试使用“重定向”在 Apache conf 中强制添加尾部斜杠,但最终生成了一个递归重定向 (http://server/foo//////...)。我还没有尝试使用 .htaccess 文件,但我怀疑可能会发生同样的事情。

但是,我在 urls.py 中尝试了同样的操作:

url(r'^$', redirect_to, {'url': '/'}), # goes to server root http://server/
url(r'^$', redirect_to, {'url': 'foo/'}), # goes to http://server/foo/foo
url(r'^$', redirect_to, {'url': '/foo/'}), # infinite redirect

我还尝试简单地在所有 Django url 上附加一个斜杠,如下所示:

url(r'^(.*)/', include(admin.site.urls))

但它无法匹配项目根文件夹中的任何内容(尽管如果您导航到该应用程序,似乎工作正常)。

我正在使用带有 mod_wsgi 的 Apache 2.2,这是配置:

Alias /static "C:/DJANGO~1/apps/django/django/contrib"

<Directory 'C:/DJANGO~1/apps/django/django/contrib'>
  Order allow,deny
  Allow from all
</Directory>

WSGIScriptAlias /foo "C:/Djangostack/apps/django/scripts/django.wsgi"

<Directory 'C:/Djangostack/apps/django/scripts'>
  Order allow,deny
  Allow from all
</Directory>

这是主要工作的 urls.py

urlpatterns = patterns('',
  url(r'^', include(admin.site.urls)),
)

我已确保 APPEND_SLASH 设置为 True,但它似乎在根项目文件夹中不起作用。

I'm trying to redirect the Django site root to the Django admin panel, and I've mostly got it up and running. However, I've noticed that unless there's a trailing slash in the project URL, the links on the page don't include the project folder, and instead try to find the apps in the site root.

So, say I've got a project foo, and an app bar. If you visit http://server/foo/ (with a trailing slash), everything works fine, and the links on the page go to http://server/foo/bar. However, if one visits http://server/foo, the generated links go to http://server/bar instead, which generates a 404 error.

If I set the WSGIScriptAlias to point to /foo/ instead of /foo, it would give a 404 error if I navigated to /foo. I tried to force a trailing slash in the Apache conf with Redirect, but I end up generating a recursive redirect (http://server/foo//////...). I haven't yet tried using a .htaccess file, but I suspect the same thing might happen.

I tried the same thing in urls.py, however:

url(r'^

I also tried simply appending a slash to all the Django urls like so:

url(r'^(.*)/', include(admin.site.urls))

But it fails to match anything at all in the project root folder (although if you navigate to the app, that seems to work OK).

I'm using Apache 2.2 with mod_wsgi, here is the configuration:

Alias /static "C:/DJANGO~1/apps/django/django/contrib"

<Directory 'C:/DJANGO~1/apps/django/django/contrib'>
  Order allow,deny
  Allow from all
</Directory>

WSGIScriptAlias /foo "C:/Djangostack/apps/django/scripts/django.wsgi"

<Directory 'C:/Djangostack/apps/django/scripts'>
  Order allow,deny
  Allow from all
</Directory>

And this is the urls.py that mostly works:

urlpatterns = patterns('',
  url(r'^', include(admin.site.urls)),
)

I've made sure APPEND_SLASH is set to True, but it doesn't seem to work in the root project folder.

, redirect_to, {'url': '/'}), # goes to server root http://server/ url(r'^

I also tried simply appending a slash to all the Django urls like so:


But it fails to match anything at all in the project root folder (although if you navigate to the app, that seems to work OK).

I'm using Apache 2.2 with mod_wsgi, here is the configuration:


And this is the urls.py that mostly works:


I've made sure APPEND_SLASH is set to True, but it doesn't seem to work in the root project folder.

, redirect_to, {'url': 'foo/'}), # goes to http://server/foo/foo url(r'^

I also tried simply appending a slash to all the Django urls like so:


But it fails to match anything at all in the project root folder (although if you navigate to the app, that seems to work OK).

I'm using Apache 2.2 with mod_wsgi, here is the configuration:


And this is the urls.py that mostly works:


I've made sure APPEND_SLASH is set to True, but it doesn't seem to work in the root project folder.

, redirect_to, {'url': '/foo/'}), # infinite redirect

I also tried simply appending a slash to all the Django urls like so:

But it fails to match anything at all in the project root folder (although if you navigate to the app, that seems to work OK).

I'm using Apache 2.2 with mod_wsgi, here is the configuration:

And this is the urls.py that mostly works:

I've made sure APPEND_SLASH is set to True, but it doesn't seem to work in the root project folder.

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

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

发布评论

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

评论(1

肤浅与狂妄 2025-01-13 03:06:50

Try setting APPEND_SLASH to True in your settings.py. I had a similar problem and this fixed it for me. It's supposed to default to True but I had to set it explicitly.

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