Django exit function removes locale settings

When I use the exit function from Django to exit an authenticated user, it switches the locale to en_US by default.

from django.contrib.auth import logout

def someview(request):
    logout(request)
    return HttpResponseRedirect('/')

How to save user language after logging out?

+3
source share
3 answers

You can try to explicitly set the language cookie when the user logs out, a little about how django determines the language setting here in djangodocs .

, , , , django .

+1

, django.contrib.auth.views.logout . - .

login urls.py:

# myproject/login/urls.py
from django.conf.urls.defaults import patterns

urlpatterns = patterns('myproject.login.views',
    ...
    (r'^logout/$', 'logoutAction'),
    ...
)

, URL/logout/ logoutAction views.py. logoutAction Django contrib.auth.views.logout.

# myproject/login/views.py
...
from django.contrib.auth.views import logout as auth_logout

def logoutAction(request):
    # Django auth logout erases all session data, including the user's
    # current language. So from the user point of view, the change is
    # somewhat odd when he/she arrives to next page. Lets try to fix this.

    # Store the session language temporarily if the language data exists.
    # Its possible that it doesn't, for example if session middleware is
    # not used. Define language variable and reset to None so it can be
    # tested later even if session has not django_language.
    language = None
    if hasattr(request, 'session'):
        if 'django_language' in request.session:
            language = request.session['django_language']

    # Do logout. This erases session data, including the locale language and
    # returns HttpResponseRedirect to the login page. In this case the login
    # page is located at '/'
    response = auth_logout(request, next_page='/')

    # Preserve the session language by setting it again. The language might
    # be None and if so, do nothing special.
    if language:
        request.session['django_language'] = language

    # Now user is happy.
    return response

LAN Quake:)

+2

UserProfile ( ) ( ). , .

Django AUTH_PROFILE_MODULE, "" django.contrib.auth

0

Source: https://habr.com/ru/post/1743300/


All Articles