Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does django locale work between views
    primarykey
    data
    text
    <p>I know django picks up default language for website from browser headers first and then displays page in that language if it matches possible choises from settings. </p> <p>What I am trying to do, is to change &amp; set that language after user has logged on. I have provided form for users for their default settings. Upon logon that default language is picked from those <code>defaultSettings</code> models. And what I am doing is:</p> <pre><code>d = DefaultSettings.objects.filter(user = request.user) if len(d) &gt; 0 and d[0].has_default_language: from django.utils import translation translation.activate(d[0].default_language) request.LANGUAGE_CODE = translation.get_language() </code></pre> <p>And what I'm seeing is page in "wrong" language. </p> <p>Which makes me ask -Why? I did not make that code up by myself. I got it from following examples</p> <ul> <li><a href="https://stackoverflow.com/questions/1527997/detect-the-language-django-locale-url">Detect the language &amp; django locale-url</a></li> <li><a href="https://github.com/Torte/django-urli18n/blob/master/urli18n/middleware.py" rel="nofollow noreferrer">https://github.com/Torte/django-urli18n/blob/master/urli18n/middleware.py</a></li> <li><a href="https://github.com/divio/django-cms/blob/develop/cms/middleware/multilingual.py" rel="nofollow noreferrer">https://github.com/divio/django-cms/blob/develop/cms/middleware/multilingual.py</a></li> </ul> <p>Since all those examples modify request/response in <code>middleware</code> - do I really have to do the same? Does <code>Django</code> reset language between requests and tries to "guess" it again after each request?</p> <p>Why does not my way of setting it once work?</p> <p>Alan</p> <p>Update after 1st response (from Sindri Guðmundsson):</p> <pre><code> if form.is_valid ( ): if not redirect_to or '//' in redirect_to or ' ' in redirect_to: redirect_to = settings.LOGIN_REDIRECT_URL if not form.cleaned_data [ 'remember_me' ]: request.session.set_expiry ( 0 ) from django.contrib.auth import login login ( request, form.get_user ( ) ) if request.session.test_cookie_worked ( ): request.session.delete_test_cookie ( ) set_lang_to_user_default_language(request) response = HttpResponseRedirect ( redirect_to ) d = DefaultSettings.objects.filter(user = request.user) if len(d) &gt; 0 and d[0].has_default_language: from django.utils import translation translation.activate(d[0].default_language) logger.debug(translation.get_language()) request.LANGUAGE_CODE = translation.get_language() if hasattr(request, 'session'): logger.debug('set django_language') request.session['django_language'] = translation.get_language() else: logger.debug('set response cookie') response.set_cookie(settings.LANGUAGE_COOKIE_NAME, translation.get_language()) return response </code></pre> <p>When I check log, I see:</p> <pre><code>DEBUG 2011-09-01 09:08:13,379 et DEBUG 2011-09-01 09:08:13,379 set django_language </code></pre> <p>But when I check template in next view, where I have <code>{{ LANGUAGE_CODE }}</code> printed out, then it shows 'en' not 'et'</p> <p>Update2 : </p> <p>Actually what happens after processing this view is : 1st page where this view redirects to {{ LANGUAGE_CODE }} is 'en' content is in English 2nd page where I go after 1st <code>{{ LANGUAGE_CODE }}</code> is 'en' but content is in Estonian 3rd page where I go after 2nd {{ LANGUAGE_CODE }} is 'en' and content is in English again and remains English from thereon.</p> <p>So it looks like I have to create my own <code>middleware</code> to keep page in "correct" language... my question is though - WHY?</p> <p>Update3 : My language settings are like that :</p> <pre><code>LANGUAGES = ( ('et', gettext('Estonian')), ('en', gettext('English')), ('ru', gettext('Russian')), ('lv', gettext('Latvian')), ('lt', gettext('Lithuanian')), ('fi', gettext('Finnish')), ) </code></pre> <p>But after further investigation I think I found a workaround. I use <code>django-cms</code> in this project and I turned off cms.middleware.multilingual.MultilingualURLMiddleware and experienced the issues I described above. When I turn it back on, then everything works just fine - but it works just fine because that <code>middleware</code> is turned on and it puts required parameters into each and every response.</p> <p>What I asked initially with my question was- how it works. And I've asked question WHY later. Now, I think, the question is - does one really have to set language for each and every <code>request/response</code> like this <code>middleware</code> does and like the example <code>middlewares</code> do? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload