Django 在域根目录下的目录中设置 WSGI:模板中的 URL 问题

发布于 2024-10-22 06:56:19 字数 1709 浏览 3 评论 0原文

Django 已经在我的 Apache mod_wsgi 实现(在 Windows 7 x64 上,顺便说一句)上启动并运行了一段时间。这是一个巨大的痛苦,必须手动修改注册表值才能正确安装,并让 Django 使用与我所有其他应用程序相同的 MySQL 安装。我还必须通过双转义括号(Program Files (x86))来修改 PATH 变量,因为它们与批处理文件有关。但这主要是微软的错,我在胡言乱语。

这是我的问题: URLCONF 和视图中使用的所有 URL 都能正常工作。当我尝试处理网站根 URL 时,唯一没有的就是模板。我正在运行一台开发服务器,并且有两个 Django 站点,其中一个是在 www.example.com/testing 上运行的。

在模板中,如果我只是将“/”放在 << a >,那么它将指向 www.example.com/,而不是 www.example.com/testing/。我读过有关此的主题,但问题没有解决,因为海报很顽固。我将我的 WSGI 配置发布在下面的 httpd.conf 中:

Listen 127.0.0.1:8001
 VirtualHost *:8001 >
    ServerName ****.net/testing
    ServerAdmin *******@gmail.com
    ErrorLog ********.log

    WSGIScriptAlias /testing ****/htdocs/testing/apache/django.wsgi

     Directory ****/htdocs/testing/apache >
        Order deny,allow
        Allow from all
     /Directory>
     Location "/media">
        SetHandler None
     /Location>
/VirtualHost>

注意:所有“<”省略,因此标签将显示

以下是上述目录中的文件 django.wsgi:

import sys
import os

sys.path.insert(0, '*****/Django/')
sys.path.insert(0, '*****/htdocs/')
sys.path.insert(0, '*****/htdocs/testing')
sys.path.insert(0, '*****/htdocs/testing/apache')

os.environ['DJANGO_SETTINGS_MODULE'] = 'testing.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

import testing.monitor
testing.monitor.start(interval=1.0)

我有一个 Nginx 前端,它将任何非静态文件从testing/传递到:8001,以便 Apache 捕获此虚拟主机。

如果我省略 django.wsgi 文件中的根“htdocs/”行,我只会在网站上收到 500 个错误。另外,如果我使用相对于当前 URL 的 URL 形式(即“example/”而不是“/example/”),那就可以了。但是,如果我添加最初的“/”以将 URL 置于根目录之外,它将使其脱离“www.example.com”,而不是像我想要的那样“www.example.com/testing”。

很抱歉这篇文章很长,但我想尽可能清楚。感谢您抽出时间。

Django has been up and running on my mod_wsgi implementation of Apache (on Windows 7 x64, btw) for a bit. This was a huge pain, complete with having to actually hand-modify registry values to get things to install correctly and to get Django to use the same MySQL install as all my other apps. I also had to modify the PATH variable by double-escaping parentheses (Program Files (x86)) because they were screwing with batch files. But this is mostly Microsoft's fault and I'm rambling.

Here is my issue:
All of the URLs used in the URLCONF and in views work correctly. The only thing which doesn't is in templates when I try to work off the site root URL. I am running a development server and I have two Django sites, this particular one running off of www.example.com/testing.

In the templates, if I just put "/" in an < a >, then it will point to www.example.com/, and NOT www.example.com/testing/. I have read a topic on this but the issue wasn't resolved because the poster was being stubborn. I am posting my WSGI configuration in the httpd.conf below:

Listen 127.0.0.1:8001
 VirtualHost *:8001 >
    ServerName ****.net/testing
    ServerAdmin *******@gmail.com
    ErrorLog ********.log

    WSGIScriptAlias /testing ****/htdocs/testing/apache/django.wsgi

     Directory ****/htdocs/testing/apache >
        Order deny,allow
        Allow from all
     /Directory>
     Location "/media">
        SetHandler None
     /Location>
/VirtualHost>

Note: all "<" omitted so the tags would show

Here is the file django.wsgi in the above directory:

import sys
import os

sys.path.insert(0, '*****/Django/')
sys.path.insert(0, '*****/htdocs/')
sys.path.insert(0, '*****/htdocs/testing')
sys.path.insert(0, '*****/htdocs/testing/apache')

os.environ['DJANGO_SETTINGS_MODULE'] = 'testing.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

import testing.monitor
testing.monitor.start(interval=1.0)

I have an Nginx frontend which passes any non-static file from testing/ to :8001 for Apache to catch with this virtualhost.

If I omit the root "htdocs/" line from the django.wsgi file, I just get 500 errors on the site. Also, if I use the URL form relative to the current URL (ie "example/" instead of "/example/"), that works alright. But if I add the initial "/" to put the URL off the root, it will make it off of "www.example.com" instead of "www.example.com/testing" like I wanted.

Sorry for the very long post, but I wanted to be as clear as possible. Thank you for your time.

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

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

发布评论

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

评论(1

橙幽之幻 2024-10-29 06:56:19

这就是为什么您不应该在模板中对 URL 进行硬编码。当然 / 会将您带到网站的根目录,而不是应用程序的根目录 - 这就是它应该做的事情。

相反,在 urlconf 中为根视图命名:

urlpatterns = patterns('',
    url('^

现在在模板中您可以执行以下操作:

<a href="{% url home %}">Home</a>

这将正确解析为 /testing/

, 'myapp.views.index', name='home') )

现在在模板中您可以执行以下操作:

这将正确解析为 /testing/

This is why you should not hard-code URLs in your templates. Of course / will take you to the root of the site, not the root of your app - that's what it's supposed to do.

Instead, give your root view a name in your urlconf:

urlpatterns = patterns('',
    url('^

now in your template you can do:

<a href="{% url home %}">Home</a>

and this will correctly resolve to /testing/.

, 'myapp.views.index', name='home') )

now in your template you can do:

and this will correctly resolve to /testing/.

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