Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django localeURL when WSGIScriptAlias is /PREFIX

Introduction

I Got a question about localeURL usage. Everything works great for me with url like this : http://www.example.com/

  • If I type http://www.example.com/ in address bar, it turns correctly in http://www.example.com/en/ for example.

  • If I use the view change_locale, it's also all right (ie change www.example.com/en/ in www.example.com/fr/).

problem

But my application use apache as server, with mod_wsgi. The httpd.conf script contains this line :

WSGIScriptAlias /MY_PREFIX /path/to/django/app/apache/django.wsgi

that gives url like this :
http://www.example.com/MY_PREFIX/

  • If I type http://www.example.com/MY_PREFIX/ in the address bar, the adress turns into http://www.example.com/en/ when the expected result should be http://www.example.com/MY_PREFIX/en/

The same problem occurred with the change_locale view. I modified this code in order to manage this prefix (store in settings.SERVER_PREFIX):

    def change_locale(request) :
    """
    Redirect to a given url while changing the locale in the path
    The url and the locale code need to be specified in the
    request parameters.
    O. Rochaix; Taken from localeURL view, and tuned to manage :            
        - SERVER_PREFIX from settings.py
    """
    next = request.REQUEST.get('next', None)
    if not next:
        next = request.META.get('HTTP_REFERER', None)
    if not next:
        next = settings.SERVER_PREFIX + '/'

    next = urlsplit(next).path

    prefix = False
    if settings.SERVER_PREFIX!="" and next.startswith(settings.SERVER_PREFIX) :
        prefix = True
        next = "/" + next.lstrip(settings.SERVER_PREFIX) 

    _, path = utils.strip_path (next)

    if request.method == 'POST':
        locale = request.POST.get('locale', None)
        if locale and check_for_language(locale):
            path = utils.locale_path(path, locale)

    if prefix :
        path = settings.SERVER_PREFIX + path

    response = http.HttpResponseRedirect(path)
    return response

with this customized view, i'm able to correctly change language, but i'm not sure that's the right way of doing stuff.

Question

  1. when, in httpd.conf you use WSGIScriptAlias with /PREFIX (ie "/Blog"), do we need, on python side to use a variable (here settings.SERVER_PREFIX) that match WSGIScriptAlias ? i use it for MEDIA_URL and other stuff, but maybe there is some configuration to do in order to make it work "automatically" without having to manage this on python side

  2. Do you think that this customized view (change_locale) is the right way to manage this issue ? Or is there some kind of automagic stuff as for 1. ?

  3. It doesn't solve the problem if I type the address (http://www.example.com/MY_PREFIX/) in address bar. If customization is the way to go, i will change this as well, but I think there is a better solution!

like image 826
Olivier Rochaix Avatar asked Jun 07 '10 09:06

Olivier Rochaix


1 Answers

You should not be hard wiring SERVER_PREFIX in settings. The mount prefix for the site is available as SCRIPT_NAME in the WSGI environ dictionary. Thus from memory is available as request.META.get('SCRIPT_NAME').

like image 109
Graham Dumpleton Avatar answered Nov 08 '22 04:11

Graham Dumpleton