Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:43:23+00:00 2026-05-25T10:43:23+00:00

I know django picks up default language for website from browser headers first and

  • 0

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.

What I am trying to do, is to change & 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 defaultSettings models. And what I am doing is:

d = DefaultSettings.objects.filter(user = request.user)
if len(d) > 0 and d[0].has_default_language:
    from django.utils import translation      
    translation.activate(d[0].default_language)
    request.LANGUAGE_CODE = translation.get_language()

And what I’m seeing is page in “wrong” language.

Which makes me ask -Why? I did not make that code up by myself. I got it from following examples

  • Detect the language & django locale-url
  • https://github.com/Torte/django-urli18n/blob/master/urli18n/middleware.py
  • https://github.com/divio/django-cms/blob/develop/cms/middleware/multilingual.py

Since all those examples modify request/response in middleware – do I really have to do the same? Does Django reset language between requests and tries to “guess” it again after each request?

Why does not my way of setting it once work?

Alan

Update after 1st response (from Sindri Guðmundsson):

    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) > 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

When I check log, I see:

DEBUG 2011-09-01 09:08:13,379 et
DEBUG 2011-09-01 09:08:13,379 set django_language

But when I check template in next view, where I have {{ LANGUAGE_CODE }} printed out, then it shows ‘en’ not ‘et’

Update2 :

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 {{ LANGUAGE_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.

So it looks like I have to create my own middleware to keep page in “correct” language… my question is though – WHY?

Update3 : My language settings are like that :

LANGUAGES = (
            ('et', gettext('Estonian')),
            ('en', gettext('English')),
            ('ru', gettext('Russian')),
            ('lv', gettext('Latvian')),
            ('lt', gettext('Lithuanian')),
            ('fi', gettext('Finnish')),
            )

But after further investigation I think I found a workaround. I use django-cms 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 middleware is turned on and it puts required parameters into each and every response.

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 request/response like this middleware does and like the example middlewares do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:43:24+00:00Added an answer on May 25, 2026 at 10:43 am

    Suggestion/answer:

    I got some weird behaviour on that also! Because I was forgetting to add the ‘LocaleMiddleware’, double check if it is there, for illustration this is how my ‘MIDDLEWARE_CLASSES’ looks like:

    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
    )
    

    That Middleware is responsible for process your request and response, and add the language!

    Also keep the code from ‘Sindri Guðmundsson’, which is changing right the lang!

    EDIT

    A little bit further! If we take a look at LocaleMiddleware:

    def process_request(self, request):
        language = translation.get_language_from_request(request)
        translation.activate(language)
        request.LANGUAGE_CODE = translation.get_language()
    

    So the question is! why Django needs that?! the answer: Because if you use {{ LANGUAGE_CODE }} it is reading from the REQUEST, so LANGUAGE_CODE must be in the request! otherwise ! it is not going to return it! so thats why this middleware exist!
    Check it out the comment, in the actual source code of the middleware:

    """
    This is a very simple middleware that parses a request
    and decides what translation object to install in the current
    thread context. This allows pages to be dynamically
    translated to the language the user desires (if the language
    is available, of course).
    """
    

    Also check the docs: https://docs.djangoproject.com/en/1.3/topics/i18n/deployment/#how-django-discovers-language-preference

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know that django uses unicode strings all over the framework instead of normal
Does anyone know how this could properly be written in Django? {{ mu.expiry_date|default:{% trans
I have a django (Python) project that needs to know what version its code
Does anyone know of a Django 1.0 + postgresql + apache + mod_python VMware
I know you can specify fieldsets in django for Admin helpers. However, I cannot
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
I know Microsoft has made efforts in the direction of semantic and cross-browser compliant
I know that I can do something like $int = (int)99; //(int) has a
I know django purposely does not allow a whole lot of logic in the
I wanted to know is Django a good choice for a big web applicatin(Social

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.