Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@login_required is losing the current specified language

Tags:

python

django

I am using i18n_patterns to internationalize my app and it's working except when I click on a link that requires login (a view protected by @login_required decorator), I am being redirected to the login form in the default language instead of the currently active one.

How I can preserve the active URL? In other words, When in the French section, I want @login_required to redirect me /fr/login/?next=/fr/clients/ instead of /en/login/?next=/fr/clients/

like image 909
Adam Silver Avatar asked Jul 23 '13 21:07

Adam Silver


2 Answers

I had the same issue, and I solved it by editing settings.py:

from django.core.urlresolvers import reverse_lazy

LOGIN_URL = reverse_lazy('login')

The function reverse_lazy adds the correct language prefix on the URL ! (with 'login' being the name of your login route)

like image 111
oliverpool Avatar answered Oct 18 '22 12:10

oliverpool


I'm not well-versed in i18n for Django, but I don't think this is actually possible because login_required binds its login_url parameter to the decorated function at the point of decorator application. You're probably better off writing your own decorator; assuming you don't use either of the optional parameters to login_required, you can make your own as

from django.contrib.auth.views import redirect_to_login
from django.core.urlresolvers import reverse
import functools

def login_required(fn):
    @functools.wraps(fn)
    def _decorated(request, *args, **kwargs):
        if request.user.is_authenticated():
            return fn(request, *args, **kwargs)

        path = request.get_full_path()
        login_url = reverse('login')
        return redirect_to_login(path, login_url)

where reverse('login') gets whatever the name of your login view in your urls.py is.

I haven't tested this, but if something comes up I'll try to debug it to the best of my ability.

like image 40
fmt Avatar answered Oct 18 '22 12:10

fmt