当WSGIScriptAlias是/ PREFIX时,Django localeURL

Oli*_*aix 8 python django mod-wsgi django-urls django-wsgi

介绍

我对localeURL用法有疑问.对于我来说,一切都很棒,如下所示:http: //www.example.com/

问题

但我的应用程序使用apache作为服务器,使用mod_wsgi.httpd.conf脚本包含以下行:

WSGIScriptAlias /MY_PREFIX /path/to/django/app/apache/django.wsgi
Run Code Online (Sandbox Code Playgroud)

给出这样的网址:http:
//www.example.com/MY_PREFIX/

change_locale视图出现了同样的问题.我修改了这段代码以便管理这个前缀(存储在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
Run Code Online (Sandbox Code Playgroud)

使用这个自定义视图,我能够正确地改变语言,但我不确定这是正确的做事方式.

  1. 当你在httpd.conf中使用WSGIScriptAlias和/ PREFIX(即"/ Blog")时,我们是否需要在python端使用与WSGIScriptAlias匹配的变量(此处为settings.SERVER_PREFIX)?我将它用于MEDIA_URL和其他东西,但也许有一些配置要做,以使其"自动"工作,而无需在python端管理它

  2. 您是否认为此自定义视图(change_locale)是管理此问题的正确方法?或者是否有某种自动化的东西,如1?

  3. 如果我在地址栏中输入地址(http://www.example.com/MY_PREFIX/),则无法解决问题.如果定制是要走的路,我也会改变它,但我认为有更好的解决方案!

ako*_*nsu 0

试试这个(我不确定它是否会起作用):

WSGIScriptAliasMatch ^/MY_PREFIX(/.*)?$ /path/to/django/app/apache/django.wsgi$1
Run Code Online (Sandbox Code Playgroud) 基本上的想法是让 django 相信没有前缀

但您需要确保 django 在其 HTML 输出中发出正确的 URL。