【问题标题】:Django localeURL when WSGIScriptAlias is /PREFIXWSGIScriptAlias 为 /PREFIX 时的 Django localeURL
【发布时间】:2011-02-28 15:00:17
【问题描述】:

简介

我有一个关于 localeURL 使用的问题。 像这样的 url 对我来说一切都很好: 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 的 WSGIScriptAlias(即“/Blog”)时,我们是否需要在 python 端使用匹配 WSGIScriptAlias 的变量(此处为 settings.SERVER_PREFIX)?我将它用于 MEDIA_URL 和其他东西,但也许需要做一些配置才能使其“自动”工作,而不必在 python 端管理它

  2. 您认为此自定义视图 (change_locale) 是管理此问题的正确方法吗?还是有一些像 1 一样的自动魔法?

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

【问题讨论】:

标签: python django django-urls mod-wsgi django-wsgi


【解决方案1】:

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

【讨论】:

  • 我不再从事这个项目,但听起来不错!
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多