我想把断网设置好.我有一个页面,页面1,只有经过身份验证的用户才能访问.当我断开连接时,我仍然可以访问页面1.
我还有另外一个问题,Django如何知道执行LOGOUT_USER函数?(我对登录也有同样的问题,但由于它的工作原理,我没有问自己这个问题^^).
既然我们已经在html中指示了重定向,为什么还要在返回中指示重定向呢?
appIdentification/views.py个
from django.contrib.auth import authenticate, login, logout
def logout_user(request):
logout(request)
messages.success(request, ("You Were Logged Out!"))
return redirect('home')
appIdentification/urls.py个
from django.urls import path
from . import views
urlpatterns = [
path('/login', views.login_user, name="login"),
path('', views.logout_user, name="logout"),
]
mainApp/template/mainApp/page1.html个
<body>
<h1> PAGE 1 </h1>
{% if user.is_authenticated %}
<a href="{% url 'login' %}"> Logout </a>
{% endif %}
</body>
mainApp/views.py个
@login_required
def page1(request):
return render(request, 'mainApp/p1.html', {})
mainApp/urls.py个
from django.urls import path, include
from . import views
path('mainApp', include('django.contrib.auth.urls')),
path('mainApp', include('appIdentification.urls')),
path('home', views.home, name="home"),
path('p1', views.page1, name="p1"),