如果您看到我的项目的urls.py,为什么登录视图没有next_page参数,而注销视图却具有该参数?我的Udemy老师没有很好地解释我对用户身份验证和用户注销时登录视图如何重定向到另一个模板的好奇心。
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('accounts/login/', views.login, name='login'),
path('accounts/logout/', views.logout, name='logout', kwargs= {'next_page':settings.LOGOUT_REDIRECT_URL}),
]
如果您在我的第二张图片中看到,则为login.html; 讲师说在我的登录模板中要求输入隐藏标签,但是他没有解释为什么?我对此行发表了评论,并且我的项目也运作良好。
{% extends 'blog/base.html' %}
{% block body_block %}
<div class="jumbotron">
<h1 class="display-3">Please Login:</h1>
<p class="lead">Must be SuperUser, please check with the site admin.</p>
<!-- form.errors return True if found any error. -->
{% if form.errors %}
<p>Your username and password didn't match! Please try again.</p>
{% endif %}
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Login">
{% comment %} <input type="hidden" name="next" value="{{next}}"> {% endcomment %}
</form>
</div>
{% endblock body_block %}
# "LOGIN_REDIRECT_URL" is the path where Django redirect to when the user authenticated.
# At less a view decide to what template to redirect. If you don't expecifies a LOGIN_REDIRECT_URL,
# Django is going to use mydomain.../accounts/login/ and for logout mydomain.../accounts/logout/
# In this case we want to redirect to the home page.
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
如果有人教给我与众不同,我将不胜感激。