Opened 13 years ago

Closed 13 years ago

#17568 closed Bug (fixed)

i18n_patterns and LOGIN_URL, LOGOUT_URL, LOGIN_REDIRECT_URL

Reported by: anonymous Owned by: neaf
Component: Documentation Version: 1.4-alpha-1
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

There is some bug with i18n_patterns and redirection after login/logout action. Sample code:

    #urls.py

    urlpatterns = i18n_patterns('',
        ...
        (_(r'^auth/'), include('apps.my_auth.urls', namespace='auth')),
    )

    #settings.py

    ...
    gettext_noop = lambda s: s

    LOGIN_URL = gettext_noop('/auth/login/')

    LOGOUT_URL = gettext_noop('/auth/logout/')

    LOGIN_REDIRECT_URL = gettext_noop('/accounts/profile/')

The urls are translated (for "en" and "nl" language). Now login at http://server.com/en/auth/login/ redirect to /en/accounts/profile/ and also http://server.com/nl/nl_auth/nl_login/ redirect to /nl/accounts/profile/ but should to /nl/nl_accounts/nl_profile/

I think there is a bug.

Thanks.

Attachments (2)

17568.diff (1.3 KB ) - added by neaf 13 years ago.
Add hint to documentation about using reverse_lazy.
17568_2.diff (1.8 KB ) - added by Zbigniew Siciarz 13 years ago.
Link to reverse_lazy. Also changed the example to use regular patterns() instead of i18n_patterns(), as these are mentioned at the end of the note.

Download all attachments as: .zip

Change History (10)

comment:1 by Julien Phalip, 13 years ago

Resolution: invalid
Status: newclosed

I think your issue comes from the fact that you're using a noop function, so your url constants will never get translated. I suggest using reverse_lazy instead. Please feel free to re-open if you disagree, but in that case please also provide an actual test case so it's easier to assess your problem.

comment:2 by anonymous, 13 years ago

Resolution: invalid
Status: closedreopened

It does not work. Django uses settings.LOGIN_URL, settings.LOGOUT_URL, settings.LOGIN_REDIRECT_URL in django.contrib.auth.views and puts them into the HttpResponseRedirect, so I nothing can do. If Django could use redirect function in place of HttpResponseRedirect this will allow to use url patterns in settings (like: LOGIN_URL = "auth:login") but it does not.

comment:3 by Julien Phalip, 13 years ago

It's unfortunate that the default values for those settings are hardcoded URLs, but changing that would be backwards-incompatible.

Have you tried redefining those settings using reverse_lazy in your project?

comment:4 by neaf, 13 years ago

Owner: changed from nobody to neaf
Status: reopenednew

comment:5 by neaf, 13 years ago

Triage Stage: UnreviewedAccepted

Ok, I tested it to be sure: reverse_lazy does what you need it to do.

comment:6 by neaf, 13 years ago

Component: Core (URLs)Documentation
Has patch: set
Status: newassigned
Triage Stage: AcceptedReady for checkin
Type: BugUncategorized

I tested it with reverse_lazy and it works fine.

from django.conf.urls import url
from django.utils.translation import ugettext as _
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns('',
    url(_(r'^home/$'), 'languages.views.home', name='home'),
    url(_(r'^login_success/$'), 'languages.views.login_success', name='login_success'),
)
urlpatterns += i18n_patterns('django.contrib.auth.views',
    url(_(r'^login/$'), 'login', name='login'),
)

My pl locale:

#: urls.py:6
msgid "^home/$"
msgstr "^dom/$"

#: urls.py:7
msgid "^login_success/$"
msgstr "^logowanie_udane/$"

#: urls.py:10
msgid "^login/$"
msgstr "^loguj/$"

My settings:

LOGIN_URL = reverse_lazy('login')
LOGIN_REDIRECT_URL = reverse_lazy('login_success')

Login required redirects to /pl/loguj/

After login redirects to /pl/logowanie_udane/

This is expected behavior. Patch with hint in documentation added (had problems with linking reverse_lazy - if someone knows more about rst please fix).

by neaf, 13 years ago

Attachment: 17568.diff added

Add hint to documentation about using reverse_lazy.

by Zbigniew Siciarz, 13 years ago

Attachment: 17568_2.diff added

Link to reverse_lazy. Also changed the example to use regular patterns() instead of i18n_patterns(), as these are mentioned at the end of the note.

comment:7 by Jannis Leidel, 13 years ago

Type: UncategorizedBug

comment:8 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: assignedclosed

In [17626]:

Fixed #17568 -- Mentioned reverse_lazy in the LOGIN_REDIRECT_URL settings docs. Thanks, zsiciarz.

Note: See TracTickets for help on using tickets.
Back to Top