Code

Opened 2 years ago

Closed 2 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 2 years ago.
Add hint to documentation about using reverse_lazy.
17568_2.diff (1.8 KB) - added by zsiciarz 2 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 Changed 2 years ago by julien

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 Changed 2 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to 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 Changed 2 years ago by julien

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 Changed 2 years ago by neaf

  • Owner changed from nobody to neaf
  • Status changed from reopened to new

comment:5 Changed 2 years ago by neaf

  • Triage Stage changed from Unreviewed to Accepted

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

comment:6 Changed 2 years ago by neaf

  • Component changed from Core (URLs) to Documentation
  • Has patch set
  • Status changed from new to assigned
  • Triage Stage changed from Accepted to Ready for checkin
  • Type changed from Bug to 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).

Changed 2 years ago by neaf

Add hint to documentation about using reverse_lazy.

Changed 2 years ago by zsiciarz

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 Changed 2 years ago by jezdez

  • Type changed from Uncategorized to Bug

comment:8 Changed 2 years ago by jezdez

  • Resolution set to fixed
  • Status changed from assigned to closed

In [17626]:

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.