简体   繁体   中英

Page not found (404) , error in django logout function

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.

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

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>

This is 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'),
 ]

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/')

Consider using url in html

Here's an example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM