简体   繁体   中英

Django: admin login with parameter

I want to have my custom " /login " page. So in settings.py I did a simple LOGIN_URL = '/login' . But before doing it, I want to develop all other more complex pages. I found a simple but very effective hack like this:

urlpatterns = [
    # blabla
    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/$', RedirectView.as_view(
            url=reverse_lazy('admin:login'))),
    # blabla
]

This means: when the user is not connected he/she is redirected to /login . In the urls, /login is converted to 'admin:login' which is admin/login . This is a "double" redirect. Everthing works fine except this:

  • origin URL: " /my_jobs "
  • redirected to " login?next=/my_jobs "
  • redirected to " /admin/login "

So my problem is that I want do pass again the " next " parameter in the RedirectView . I found a lot about redirection and custom login, but not something about that on stackoverflow (this is not a duplicate).

You can set query_string to True , so that query strings are appended to the URL.

RedirectView(
    url=reverse_lazy('admin:login'),
    query_string=True,
    # You might want to set permanent=False, 
    # as it defaults to True for Django < 1.9
    permanent=False, 
)

Note that Django comes with a built in login view. You can enable it by adding the URL pattern and a simple template, which isn't much more work than your code above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM