本文介绍了找不到页面(404),Django注销功能出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击主页(html页面)中的注销"链接时,出现此错误:

When i click on 'logout' link in home page(html page) ,i get this error:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/login1/logout
Using the URLconf defined in webapp.urls, Django tried these URL patterns, in this order:

admin/
Home/ [name='Home']
login/ [name='login']
Signup/ [name='Signup']
login1/ [name='login1']
logout/ [name='logout']
The current path, login1/logout, didn't match any of these.

我不明白为什么在views.py文件中找不到注销"功能.

i don't understand why it can't find 'logout' function in views.py file.

这是Home.html

This is Home.html

<body>
<center>
<h1  style="color:white">Welcome to my web page</h1>
<a href= 'logout' target="_blank">LogOut</a>
</center>
</body>

这是urls.py:

from django.contrib import admin
from django.urls import path
from application import views
urlpatterns = [
     path('admin/', admin.site.urls),
     path('Home/', views.Home , name = "Home"),
     path('login/', views.login,name = 'login'),
     path('Signup/', views.Signup , name = 'Signup'),
     path('login1/',views.login1,name='login1'),
     path('logout/',views.logout,name='logout'),
 ]

这是views.py:

This is views.py:

from django.contrib import auth
from django.contrib.auth.models import User
from django.shortcuts import render, redirect

def login(request):
    return render(request, 'login.html')

def login1(request):
    if request.method == "POST":
        uname = request.POST['username']
        pwd = request.POST['pass']
        user = auth.authenticate(username=uname, password=pwd)
        if user is not None:
            auth.login(request, user)
            return render(request, 'Home.html')
        else:
            return render(request, 'login.html', {'error': 'invalid credential details'})
    else:
        return render(request, 'Signup.html')

def logout(request):
    auth.logout(request)  # logout is predefined
    return redirect('/login/')

推荐答案

考虑使用 html

Consider using url in html

这是一个例子

<a href="{% url 'logout' %}">Logout</a>

这篇关于找不到页面(404),Django注销功能出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!