1

我没有使用任何中间件,当我单击主页模板上的注销按钮时,注销功能执行没有任何错误。但是当我返回主页而不跳转到登录页面时..我认为自己是登录用户

这是我的 authentiCation/views.py

from django.shortcuts import render
from django.http import request,HttpResponseRedirect
# for user creation & login form
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login
# for user related Queries
from django.contrib.auth.models import User


# imports for test purpose
from django.http import HttpResponse

# Create your views here.

# register page 
def register_Page(request):
    if request.method == 'POST':
        form= UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username= request.POST['username']
            password= request.POST['password1']
            user= authenticate(request,username=username,password=password)
            login(request,user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponse('Either the user name is not available or you may have filled the form incorrectly')
    else:
        form = UserCreationForm()
        context= {'form':form}
        return render(request,'authentication/register_Page.html',context)

# login page
def login_page(request):
    if request.method == 'POST':
        username= request.POST['username']
        password= request.POST['password']
        # returns user if credentials are valid
        user= authenticate(request, username=username, password= password)
        # check if user var contains the user
        if user is not None:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponse('Invalid credentials')

    return render(request,'authentication/login.html')

# logout Page
def log_out(request):
    logout(request)
    return HttpResponseRedirect('logout_page')

身份验证/urls.py

from django.urls import path
from authentiCation import views

urlpatterns = [
    path('register/',views.register_Page),
    path('login/',views.login_page,name='login_page'),
    path('logout/',views.log_out),
]

主要应用程序文件 网址

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('App_wfi_Community.urls')),
    path('Ask/',include('askQuestion.urls')),
    path('auth/',include('authentiCation.urls')),
]

主页.html

{% extends "basic.html" %}
{% load humanize %}
{% block title %}WFI-Community{% endblock title %}
{% block body %}

<!--  search button   -->
<h5>Welcome {{username}},<h5> <!--  I see my username here   -->
<form action="/search" method="get">
<div class="container py-3 row">
  <div class="col-md-8 offset-2">
    <div class="input-group">
      <input name="searchfieldText" type="text" class="form-control" placeholder="Search">
      <button class="btn btn-danger" type="submit">Search</button>
      <span><a href="{% url 'login_page' %}">Logout</a></span>
    </div>
  </div>
</div>
</form>
<!--  and some more HTML stuff which is irrelavent here -->
4

1 回答 1

1

给它命名,例如 path('logout/',views.log_out, name="logout" ) 并在 home.html 中更改它。例如 "{% url '注销' %}">注销

于 2021-04-23T05:12:34.397 回答