开发者

Language specific redirection

开发者 https://www.devze.com 2022-12-24 15:06 出处:网络
I want to ignore the post form in the django\'s internatonalization. I am using the django-multilingual app, so I have different fields for different languages in the db.

I want to ignore the post form in the django's internatonalization.

I am using the django-multilingual app, so I have different fields for different languages in the db.

I come up with this idea: For each language, from the index.html page, 开发者_高级运维redirect to a different url (e.g. /en/ or /de/ or /zh/). And each view of this urls, set the session according to the language like this:

def set_lang_en(request):
   request.session['django_language'] = 'en'
   render_to_response("home.html")

def set_lang_zh(request):
   request.session['django_language'] = 'zh-cn'
   render_to_response("home.html")

Interestingly, this does the job, but if i refresh the page again after redirection (home.html).

Why it is like this? And how can solve this problem either in my direction or other one?


multilingual.middleware reads language code from session long before you set it in your view. You can change language in your view once more:

from multilingual.languages import set_default_language
set_default_language('en')

Or if you only want to get rid of method post do not render home template in your view but set cookie and redirect user back.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号