WSGIScriptAlias 为 /PREFIX 时的 Django localeURL

发布于 2024-09-04 17:39:06 字数 2901 浏览 5 评论 0原文

简介

我有一个关于 localeURL 使用的问题。 一切都很适合我,网址如下: http://www.example.com/

问题

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

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

给出如下 url:
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端使用匹配的变量(此处为settings.SERVER_PREFIX) WSGIScriptAlias ?我将它用于 MEDIA_URL 和其他东西,但也许需要做一些配置才能使其“自动”工作,而不必在 python 端管理它

  2. 你认为这个自定义视图(change_locale)是正确的如何处理这个问题?或者是否有某种类似于 1. 的自动魔法?

  3. 如果我输入地址(http://www.example.com /MY_PREFIX/)在地址栏中。如果定制是可行的方法,我也会更改它,但我认为有更好的解决方案!

Introduction

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

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/

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-09-11 17:39:07

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

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').

少女的英雄梦 2024-09-11 17:39:07

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

WSGIScriptAliasMatch ^/MY_PREFIX(/.*)?$ /path/to/django/app/apache/django.wsgi$1

basically the idea s to make django believe that there is no prefix

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

try this (I am not sure whether it will work though):

WSGIScriptAliasMatch ^/MY_PREFIX(/.*)?$ /path/to/django/app/apache/django.wsgi$1


basically the idea s to make django believe that there is no prefix

but you need to make sure django emits the correct URLs in its HTML output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文