Friday, November 4, 2011

I18N and caching: page don't change language until I press F5 in browser

Hi,
I've got a Django website that's multi-lingual, and I'd like to use memcaching on it. While everything works fine with caching disabled, I have observed the following when caching is enabled:

1) Open any page on the website
2) Click on link (the image of a little flat) to change language
3) This runs a view that sets the language in the session and the cookie, and then redirects to the referrer
4) Observe that the page is still in the original language!
5) Press F5 in the browser
6) Observe that the page is now in the language I wanted

Obviously I'm expecting the page to change language at point 4 (and that's the way it works without caching.

Now let me how you some relevant code:

My middlewares:

MIDDLEWARE_CLASSES = (                                                                                         
    'django.middleware.cache.UpdateCacheMiddleware', # KEEP AT THE BEGINNING                                   
    'django.middleware.http.ConditionalGetMiddleware',                                                         
    'django.middleware.common.CommonMiddleware',                                                               
    'django.contrib.sessions.middleware.SessionMiddleware',                                                    
    'django.contrib.auth.middleware.AuthenticationMiddleware',                                                 
    'django.middleware.cache.CacheMiddleware',                                                                 
    'django.middleware.locale.LocaleMiddleware',                                                                                                                     
    'django.contrib.messages.middleware.MessageMiddleware',                                                                                                         
    'privatebeta.middleware.PrivateBetaMiddleware',                                                            
    'django.middleware.cache.FetchFromCacheMiddleware', # KEEP AT THE END                                      


The view that changes language:

@require_GET
def set_language(request, lang):
    from django.utils.translation import check_for_language, activate

    next = request.REQUEST.get('next', None)
    if not next:
        next = request.META.get('HTTP_REFERER', None)
    if not next:
        next = '/'
    response = HttpResponseRedirect(next)
    if lang and check_for_language(lang):
        if hasattr(request, 'session'):
            request.session['django_language'] = lang
        else:
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang)
        activate(lang)

    if request.user.is_authenticated():
        profile = UserProfile.objects.get(user = request.user)
        profile.language = lang
        profile.save()

    return response


I have tried changing the order of the middleware, but to no avail.

Can somebody please help?

Thanks!
  Salvatore.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ha0fhmNQZZgJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment