编辑:所以似乎注销/ loginRequiredMixin
功能在我的'/ admin'页面中工作,所以我认为问题在于我的index.html中的按钮
我无法在我的django应用中退出我的帐户。或者,如果我,我可以去一个需要登录的视图。
换句话说,我能够正确登录,但是当我尝试添加注销功能时,它似乎什么都不做,或者LoginRequiredMixin
无法正常工作
views.朋友
class LoginView(TemplateView):
template_name = 'APP/login.html'
def post(self, request):
email = password = ""
state = ""
if request.POST:
email = request.POST.get('email')
password = request.POST.get('password')
print(email, password)
user = authenticate(username=request.POST.get('email'), password=request.POST.get('password'))
if user is not None:
login(request, user)
return redirect('/login/index/')
else:
state = "Inactive account"
# logging.StreamHandler
return render(request, self.template_name, {'state': state, 'email': email})
class LogOutView(TemplateView):
template_name = 'APP/logout.html'
def logout(self, request):
logout(request)
return redirect('/login')
class IndexView(LoginRequiredMixin,TemplateView):
login_url = '/login/'
template_name = 'APP/index.html'
我目前在我的urls.py中有这些
app_name = 'app'
urlpatterns = [
path('logout/', views.LogOutView.as_view(), name='logout'),
path('login/', views.LoginView.as_view(), name='login'),
path('login/index/', views.IndexView.as_view(), name='index'),
settings.朋友
LOGIN_URL = '/login'
LOGOUT_REDIRECT_URL = '/logout'
我的注销按钮在我的index.html页面上,如下所示:
<a href="logout/">
<button>logout</button>
</a>
我不知道如何实现LoginRequiredMixin
工作,单击注销按钮似乎没有注销用户。
您需要覆盖get
方法:
class LogOutView(TemplateView):
template_name = 'APP/logout.html'
def get(self, request, *args, **kwargs):
logout(request)
return redirect('/login')