简体   繁体   English

在Django登录后重定向用户

[英]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.我还没有完成LoginView,因为我只是想看看它是否可以在登录后直接进入管理页面。 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.我应该在 urls.py 或 LOGIN_URL/LOGIN_REDIRECT_URL 中更改或添加什么。 Thank you!谢谢!

url.py网址.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视图.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设置.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_URL是用于将用户重定向到登录页面的设置,例如,在使用login_required()装饰器时。

LOGIN_REDIRECT_URL is used for redirecting users after a successful login attempt. LOGIN_REDIRECT_URL用于在成功登录尝试后重定向用户。

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.但是, LOGIN_REDIRECT_URL只允许一个重定向 URL,因此对于您的用例,我建议在登录视图中单独设置它。 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.但是,这省略了在请求中使用next参数的功能,因此请注意,除非您特别添加,否则这是一种权衡。

I know two ways to rediretc after login/logout in django:我知道在 django 中登录/注销后重定向的两种方法:

1. You can redirect after login/logout in html templates. 1.您可以在html模板中登录/注销后重定向。 You just need to use the next parametr as follow:您只需要使用next参数如下:

{% 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)所以,如果你的员工是 admin 你会去admin_page ('admin_page/') 如果你想注销,你会去 name after_logout页面(我刚刚编的)

Or或者

2. You need to add to settings.py 2.你需要添加到settings.py

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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