介绍
我有一个关于localeURL使用的问题。 对于像这样的url,一切都很好: http://www.example.com/
如果我在地址栏中输入http://www.example.com/,它会正确地转换为例如http://www.example.com/en/。
如果我使用视图change_locale,也是没问题的(即将www.example.com/en/更改为www.example.com/fr/)。
问题
但我的应用程序使用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/MY_PREFIX/en/,但实际上它会变成http://www.example.com/en/
更改语言设置时也遇到了同样的问题。我修改了这段代码以管理这个前缀(存储在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中使用WSGIScriptAlias和 /PREFIX(即“/Blog”)时,我们是否需要在Python侧使用与WSGIScriptAlias匹配的变量(这里是settings.SERVER_PREFIX)?我将其用于MEDIA_URL和其他内容,但可能需要进行某些配置,以使其“自动”工作,而无需在Python侧进行管理。
你认为这个定制的视图(change_locale)是管理这个问题的正确方式吗?还是有一些像1.那样的自动化东西?
如果我在地址栏中输入地址(http://www.example.com/MY_PREFIX/),它并不能解决这个问题。如果定制是前进的道路,我也将改变这一点,但我认为有更好的解决方案!