首页 > 解决方案 > 如何修复 django 中的 Page Not Found (404) 错误

问题描述

如何在 django 3.0 中修复此错误,当我尝试使用表单发布方法访问页面时,我收到此错误

嗨,我是 Django 编程语言的新手,现在我正在用 django 开发一个大学项目,当我使用表单在POST方法中创建登录时。如果我把它放在主页上,它工作得很好。但是当我将表单放入另一个页面(about.html)时,我收到了这个错误。我无法修复它。所以大家请帮帮我..

> Page not found (404) Request Method:  POST Request
> URL:  http://127.0.0.1:8000/about/login Using the URLconf defined in
> mysite.urls, Django tried these URL patterns, in this order:
> 
> [name='home'] login/ about/ admin/ The current path, about/login,
> didn't match any of these.
> 
> You're seeing this error because you have DEBUG = True in your Django
> settings file. Change that to False, and Django will display a
> standard 404 page.

应用程序/ Urls.py

from django.urls import path
from . import views
urlpatterns = [
path('',views.home,name='home'),
path('', views.about, name="about"),
path('', views.login, name="login")
path('admin/', admin.site.urls),
]

项目/ urls.py

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


from pages.views import home, about, login
urlpatterns = [
path('',home,name='home'),
path('login/',login),
path('about/',about),
path('admin/', admin.site.urls),
]

视图.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
# Create your views here.

def home(request, *args, **kwargs):
    return render(request, "test.html")

def about(request,*args, **kwargs):
    return render(request,"inner.html")

def login(request, *args, **kwargs):
    return render(request, "result.html")

HTML 表格

<form action="login" method="POST">
        {%  csrf_token %}
        <label> username</label>
        <input type="text" name="uname"><br>
        <label> Password</label>
        <input type="Password" name="upwd"><br>
        <input type="submit" name="submit" value="submit">
</form> 

标签: pythondjangopython-3.xdjango-formsdjango-views

解决方案


action="login" 你在第一次写的主要问题是action="/login"为了解决你的问题,因为现在你添加login到当前打开的 url


推荐阅读