Why second user login redirects me to /accounts/profile/ url?

前端 未结 7 2481
忘掉有多难
忘掉有多难 2021-02-03 19:13

I am using Django built in view for user login:

url(r\'^user/login/$\', \'django.contrib.auth.views.login\', {\'template_name\': \'users/templates/login.html\'},         


        
7条回答
  •  萌比男神i
    2021-02-03 19:32

    @CpS provided very good solution, I just want to enhance it a little bit:

    For example, you have following urls:

    urlpatterns = [
        # django's login/logout
        url(r'login/$', 'django.contrib.auth.views.login', name='login'),
        url(r'logout/$', 'django.contrib.auth.views.logout', name='logout'),
        url(r'logout-then-login/$', 'django.contrib.auth.views.logout_then_login', name='logout_then_login'),
        url(r'^$', views.dashboard, name='dashboard')
    ]
    

    and dashboard.html is where you want to redirect after successful login, then you can modify your settings.py file accordingly:

    from django.core.urlresolvers import reverse_lazy
    
    LOGIN_REDIRECT_URL=reverse_lazy('dashboard')
    LOGIN_URL=reverse_lazy('login')
    LOGOUT_URL=reverse_lazy('logout')
    

    We are using reverse_lazy() to build the URLs dynamically. The reverse_lazy() function reverses URLs just like reverse() does, but you can use it when you need to reverse URLs before your project's URL configuration is loaded.

提交回复
热议问题