开发者

Django set up with WSGI in directory below the domain root: Url problems in templates

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 hav开发者_开发问答e 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.


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('^$', 'myapp.views.index', name='home')
)

now in your template you can do:

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

and this will correctly resolve to /testing/.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜