简体   繁体   中英

Redirect User After Login in Django

I am trying to implement my login page, so it can redirect to specific page in the websites. When the user click on the Login, it check if the user is staff, then send the user to admin page, not user then stay at login, if regular user, then send to regular user page. I haven't completed the LoginView yet because I just want to see if it can direct to admin page after login. I have researched online and tried many different way, but I couldn't figure out how to do so. What should I change or add in the urls.py or LOGIN_URL/LOGIN_REDIRECT_URL. Thank you!

url.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('output/', views.output, name="output"),
    path('default_crawler/', views.default_page, name="default_page"),
    path('admin_page/', views.admin_page, name="admin_page"),
    path('regular_page/', views.regular_page, name="regular_page"),
    path('pass_forgot/', views.pass_forgot, name="pass_forgot"),
    path('pass_change/', views.pass_change, name="pass_change"),
    path('pass_change_success/', views.pass_change_success,
         name="pass_change_success"),
    path('pass_found/', views.pass_found, name="pass_found"),
    path('register/', user_views.register, name="register"),
    path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

views.py

def LoginView(request):
    if request.method == 'POST':
        form = AuthenticationForm(request, request.POST)

        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)

            if user.is_staff:
                login(request, user)
                return redirect('admin_page')
            else:
                messages.error(request, "Invalid username or password")
        else:
            messages.error(request, "Invalid username or password")
    form = AuthenticationForm()
    return render(request, 'users/login.html', {'form': form})

settings.py

LOGIN_URL
LOGIN_REDIRECT_URL

LOGIN_URL is the setting used to redirect users to the login page when, for instance, the login_required() decorator is used.

LOGIN_REDIRECT_URL is used for redirecting users after a successful login attempt.

Source

However, LOGIN_REDIRECT_URL only allows for one redirect URL, so for your use case I would advice to set it separately in the login view instead. However , this omits the functionality of using the next parameter in your requests, so be aware that this is a tradeoff unless you specifically adds it.

I know two ways to rediretc after login/logout in django:

1. You can redirect after login/logout in html templates. You just need to use the next parametr as follow:

{% if user.is_authenticated %}          
    <a href="{% url 'logout' %}?next={% url 'app_name:after_logout' %}">{{ some_username }}</a>
{% else %}
    <a href="{% url 'login' %}?next={% url 'app_name:admin_page' %}">Login</a>
{% endif %}

So, if your staff is admin you will go to admin_page ('admin_page/') If you want to logout, you will go to page with name after_logout (I just made it up)

Or

2. You need to add to settings.py

LOGOUT_REDIRECT_URL = 'after_logout/'
LOGIN_REDIRECT_URL = 'admin_page/'

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