hi,
I've got a menu to switch languages in my Django template. I call, via POST, the "/i18n/setlang/" view shown below to change the language setting. When I use a html form the user is redirected as expected, but when I use jQuery.post(), it is not.
What am I missing?
Thanks
Call to the view using jQuery.post():
$.post("/i18n/setlang/", { language: 'en', next: "{{request.path}}" });
The view is the following:
def set_language(request):
"""
Redirect to a given url while setting the chosen language in the
session or cookie. The url and the language code need to be
specified in the request parameters.
Since this view changes how the user will see the rest of the site, it must
only be accessed as a POST request. If called as a GET request, it will
redirect to the page in the request (the 'next' parameter) without changing
any state.
"""
next = request.REQUEST.get('next', None)
if not next:
next = request.META.get('HTTP_REFERER', None)
if not next:
next = '/'
response = http.HttpResponseRedirect(next)
if request.method == 'POST':
lang_code = request.POST.get('language', None)
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session['django_language'] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
return response