Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django中的注销功能未按预期工作_Django_Django Views_Django Forms_Django Authentication_Django Auth Models - Fatal编程技术网

Django中的注销功能未按预期工作

Django中的注销功能未按预期工作,django,django-views,django-forms,django-authentication,django-auth-models,Django,Django Views,Django Forms,Django Authentication,Django Auth Models,我没有使用任何中间件,当我单击主页模板上的“注销”按钮时,注销功能将正常执行。但是当我返回主页而不跳转到登录页面时。。我将自己视为登录用户 这是我的身份验证/views.py 身份验证/url.py 主要应用程序文件 网址 Home.html {%extends“basic.html”%} {%load humanize%} {%block title%}WFI社区{%endblock title%} {%block body%} 欢迎{{username}}, 搜寻 给这个例子起个名字 路径(

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

这是我的身份验证/views.py

身份验证/url.py

主要应用程序文件 网址

Home.html

{%extends“basic.html”%}
{%load humanize%}
{%block title%}WFI社区{%endblock title%}
{%block body%}
欢迎{{username}},
搜寻
给这个例子起个名字 路径('logout/',views.log_out,name=“logout”)并在home.html中更改它。
例如“{%url'注销“%}”>注销

您如何知道单击注销链接执行注销功能?链接是登录页面。@BradMartsberger实际上我忘了。。现在我已经纠正了这一点,效果非常好,但现在的问题是URL一直像这样附加
http://127.0.0.1:8000/logout/login_page
但是我想要这样的url=>用于注销页面=
http://127.0.0.1:8000/logout
登录页面=
http://127.0.0.1:8000/login_page
这是唯一的办法。必须是这样的。如果它解决了你的问题,就接受它作为答案。但是我对URL做了什么。。我是初学者,不知道如何处理这件事。。
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')
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')),
]

{% 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 -->