WSGIScriptAlias 为 /PREFIX 时的 Django localeURL
简介
我有一个关于 localeURL 使用的问题。 一切都很适合我,网址如下: http://www.example.com/
如果我输入 http://www.example.com/ 在地址栏中,它在 http://www.example.com/en/ 例如。
如果我使用视图change_locale,也可以(即在www.example.com/fr/中更改www.example.com/en/)。
问题
但是我的应用程序使用apache作为服务器,并带有mod_wsgi。 httpd.conf 脚本包含以下行:
WSGIScriptAlias /MY_PREFIX /path/to/django/app/apache/django.wsgi
给出如下 url:
http://www.example.com/MY_PREFIX/
- 如果我输入 http://www.example.com/MY_PREFIX/,地址变为http://www.example.com/en/ 当预期结果应为 http://www.example.com/MY_PREFIX/en/
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
使用此自定义视图,我能够正确更改语言,但我不确定这是正确的做法。
问题
当在httpd.conf中使用带有/PREFIX(即“/Blog”)的WSGIScriptAlias时,我们是否需要在python端使用匹配的变量(此处为settings.SERVER_PREFIX) WSGIScriptAlias ?我将它用于 MEDIA_URL 和其他东西,但也许需要做一些配置才能使其“自动”工作,而不必在 python 端管理它
,你认为这个自定义视图(change_locale)是正确的如何处理这个问题?或者是否有某种类似于 1. 的自动魔法?
如果我输入地址(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/
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
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
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. ?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该在设置中硬连接 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').
试试这个(我不确定它是否会起作用):
<代码>
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):
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.