Django redirects missing out WSGIScriptAlias part of url
I have a django application set up using mainly the admin interface with a few of my own views. Everything is working corrctly开发者_开发问答 on the devlopment server on my local machine.
However on the production server, using Apache, and mod_wsgi, it is messing up the URLs when I issue a redirect statement in one of my views. Instead of going to
www.hostname.com/wsgi/django/admin/myapp/myform.html
it goes to
www.hostname.com/admin/myapp/myform.html
Apache doesn't recognise anything under an admin folder, so I get an error.
So in my apache configuration I have the following, so that anything under wsgi/django gets sent to my app:
WSGIScriptAlias /wsgi/django /project/production/public/wsgi/django_eclipse/myapp.wsgi
In my problematic view I have the following:
return redirect('/admin/sequencing/load_flowcell?' + params )
I can make this work, if I change the redirect to include the '/wsgi/django' part, but this means I can't keep the one on my development server the same. Is there a way in Django settings, or http.conf to set this so that my redirect automatically includes this part of the URL?
Django can use the sites framework to generate correct urls when reversing -- if you change the domain
field in the Site
row associated with your site (as indicated by SITE_ID
in your settings.py
) to http://www.hostname.com/wsgi/admin
, then everything should work fine.
(There may also be a few places where you have plain urls, e.g. the LOGIN_URL
or LOGIN_REDIRECT_URL
settings that you should make sure include the /wsgi/admin
part.)
Well, it's redirecting to exactly the URL that you told it to.
This is why you should never hard-code URLs anywhere in Django. If you used the URL-reversing functionality, it would automatically add the WSGI prefix - it knows about it already, and takes it into account when you use the {% url %}
tag or the reverse()
function.
Give the URL a name in your urls.py, and use that in the call to redirect rather than the URL, and all should work fine. No need for more settings.
EDIT: REAL FIX
from django.core.urlresolvers import reverse, get_script_prefix
....
#only of you absolutely HAVE to have a hardcoded url
return redirect(get_script_prefix()+'admin/sequencing/load_flowcell?'+params )
#better!
return redirect(reverse('admin:index')+sequencing/load_flowcell?'+params )
CAVEAT: https://docs.djangoproject.com/en/dev/howto/deployment/modpython/#basic-configuration
<Location "/mysite/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonOption django.root /mysite <---- Make sure you have this
PythonDebug On
</Location>
精彩评论