8

介绍

我有一个关于 localeURL 使用的问题。像这样的网址对我来说一切都很好: http ://www.example.com/

问题

但是我的应用程序使用 apache 作为服务器,带有 mod_wsgi。httpd.conf 脚本包含这一行:

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

给出这样的网址:
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

使用此自定义视图,我能够正确更改语言,但我不确定这是正确的做事方式。

问题

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

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

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

4

2 回答 2

3

您不应该在设置中硬连线 SERVER_PREFIX。该站点的挂载前缀在 WSGI environ 字典中以 SCRIPT_NAME 的形式提供。因此,内存中的 request.META.get('SCRIPT_NAME') 可用。

于 2011-09-03T16:52:23.720 回答
0

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

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

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

于 2011-09-03T13:26:34.443 回答