Opened 14 years ago
Closed 14 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)
Change History (10)
comment:1 by , 14 years ago
| Resolution: | → invalid | 
|---|---|
| Status: | new → closed | 
comment:2 by , 14 years ago
| Resolution: | invalid | 
|---|---|
| Status: | closed → reopened | 
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 , 14 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 , 14 years ago
| Owner: | changed from to | 
|---|---|
| Status: | reopened → new | 
comment:5 by , 14 years ago
| Triage Stage: | Unreviewed → Accepted | 
|---|
Ok, I tested it to be sure: reverse_lazy does what you need it to do.
comment:6 by , 14 years ago
| Component: | Core (URLs) → Documentation | 
|---|---|
| Has patch: | set | 
| Status: | new → assigned | 
| Triage Stage: | Accepted → Ready for checkin | 
| Type: | Bug → Uncategorized | 
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 , 14 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 , 14 years ago
| Type: | Uncategorized → Bug | 
|---|
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.