Search code examples
djangoapachemod-wsgidjango-wsgi

Moving Django site to another server- WSGIScriptAlias causing problems


I'm in the process of moving a django site over to a new server. On the old server, the django site was accessed like mysite.com/ , but now, we would like to access it via mysite.com/mysite, and let mysite.com handle something else. I have made the following changes to apache like so:

    WSGIDaemonProcess mysite processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup mysite
    WSGIScriptAlias /mysite /srv/www/django.wsgi
    #WSGIScriptAlias /mysite /srv/www/django.wsgi  #previous config         

    <Directory /srv/www/mysite/mysite >
            Order allow,deny
            Allow from all
    </Directory>

    Alias /site_media "/srv/www/mysite/site_media/"
    Alias /admin_media "/srv/www/mysite/admin_media/"

This seems to work fine- pointing the browser at mysite.com/unity/admin allows me to access the admin page correctly, and view the respective apps correct. However, anything that uses a custom template seems to be half-baked. For instance, there's an entry in a template below like so:

 {% ifcodable cl.model %}<li><a href="/report/{{ app_label }}/{{ cl.opts.verbose_name }}" class="link">Coding Report</a></li>{% endifcodable %}

This will redirect the page to

http://mysite.com/report/texas/texas

As opposed to

http://mysite.com/mysite/report/texas/texas

I'm not sure if the template is set up incorrectly or if it has something to do with the new alias. My urls.py looks like so:

from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
    .....
    (r'^report/([a-zA-Z]+?)/([a-zA-Z]+?)/(overall|\d+)/{0,1}$', 'mysite.k.views.performance'),
    (r'^report/(.+?)/(.+?)/{0,1}$', 'mysite.k.views.report'),
    .....

My django.wsgi file looks like so:

import os,sys
sys.path.append('/srv/www/mysite')
sys.path.append('/srv/www/mysite/mysite')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I don't know what the proper thing would be to do to correct the problem. I'm fairly new to django, so if there is a wicked simple solution I apologize. Any advice would be greatly appreciated!


Solution

  • The problem is that your url begins with /report in the href attribute. Beginning with a / means that it should be relative to the root of your domain. I believe what you need to change is how the Django sites framework is configured, and it looks like this question has the answer.

    Also, you should probably use the url tag in your templates instead of hardcoding the urls in your anchor tags.