繁体   English   中英

Django 不会使用内置登录视图重定向到下一页

[英]Django not redirecting to next page with in-built login view

我不知道我的代码发生了什么,一旦未经身份验证的用户尝试访问页面并重定向到登录页面,我可以在浏览器中看到下一个参数,但在用户登录后,它需要用户到默认的 LOGIN_REDIRECT_URL 而不是下一页。 同样在模板上 next 不返回任何值,因此模板似乎没有获得下一个值,但在 url 中它显示了 /?next=,提前致谢。

网址.py

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from blog import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.HomeView.as_view(),name="index"),
    path('accounts/login/', auth_views.LoginView.as_view(), name='login'),
    path('accounts/logout/', auth_views.LogoutView.as_view(next_page='/'), name='logout'),
    path('accounts/profile/', views.ProfileView.as_view(), name='profile'),
]

注册/登录.html

{% extends 'app/base.html' %}

{% block link %} id="active" {%endblock%}

{% block content %}
<div class="row">
    <div class="col-md-3">

    </div>
    <div class="col-md-6">
        <div class="jumbotron" id="font">

            {% if next %}
                {% if user.is_authenticated %}
                <h2 class="customtext" align="center">Your account doesn't have access to this page. To proceed,
                please login with an account that has access.</h2>
                {% else %}
                <h2 class="customtext" align="center">Please login.</h2>
                {% endif %}
            {% else %}
            <h2 class="customtext" align="center">Enter your login details.</h2>
            {% endif %}
            <form action="{% url 'login' %}" method="POST">
                {%csrf_token%}
                <div class="form-group">
                    <input type="text" name="username" placeholder="username" class="form-control">
                </div>
                <div class="form-group">
                    <input type="password" name="password" placeholder="password" class="form-control">
                </div>
                <!-- <div align="center"> -->
                    {% if form.errors %}
                        <!-- <p style="color: red; font-style: italic;">Your username or email is does not match.</p> -->
                        {{form.errors}}
                    {% endif %}
                <!-- </div> -->
                <div class="loginbtndiv" align="center">
                    <input type="submit" class="btn btn-warning btn-lg loginbtn" value="LOGIN">
                </div>
                <input type="hidden" name="next" value="{{ next }}">
            </form>

        </div>
    </div>
    <div>

    </div>
</div>

{% endblock %}

在 settings.py 文件中使用LOGIN_REDIRECT_URL

暂无
暂无

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

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