找不到页面 (404) 当前路径 register/POST 与这些中的任何一个都不匹配
Page not found (404) The current path, register/POST, didn't match any of these
我正在 PyCharm 编辑器上通过 Django 创建博客,我遇到了 404 错误。
这是完整的错误信息:
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
admin/
register/ [name='register']
[name='blog-home']
about/ [name='blog-about']
The current path, register/POST, didn't match any of these.
这是主项目文件夹中的urls.py文件夹:
下面的 'users' 和 'views' 红色下划线
from django.contrib import admin
from django.urls import path, include
from *users* import *views* as user_views
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name='register'),
path('', include('blog.urls')),
]
这是我的 views.py,在我的 users 文件夹中:
从django.shortcuts导入渲染,重定向
从 django.contrib.auth.forms 导入 UserCreationForm
from django.contrib import messages # 如果表单数据正确则显示消息
def register(request):
if request.method == 'POST': # if the request is a 'post' then it will create a form
form = UserCreationForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
messages.success(request, f'Account created for {username}!')
return redirect('blog-home')
else:
form = UserCreationForm() # anything not a post request it will create a blank form
return render(request, 'users/register.html', {'form': form})
以及 templates 内 users 目录中的 html 文件 register ]文件夹:
{% extends "blog/base.html" %}
{% block content %}
<div class=content-section">
<form action="Post">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today!!!</legend>
{{ form }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Sign Up</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">Already have an account? <a class="ml-2" href="#"></a>
</small>
</div>
</div>
{% endblock content %}
这是博客目录中的admin.py:
from django.contrib import admin
from . models import Post
admin.site.register(Post)
这是结构:
C:.
├───blog
│ ├───migrations
│ │ └───__pycache__
│ ├───static
│ │ └───blog
│ ├───templates
│ │ └───blog
│ └───__pycache__
├───django_project
│ └───__pycache__
└───users
├───migrations
│ └───__pycache__
├───templates
│ └───users
└───__pycache__
我已经尝试编辑代码等但没有发生变化。
如果您需要更多代码,请告诉我
请帮忙,谢谢
问题是您的表单有 action="Post"
,这导致浏览器访问不存在的 URL。
<form action="Post">
您应该使用 method="post"
让浏览器使用 POST 表单请求。
<form method="post">
我正在 PyCharm 编辑器上通过 Django 创建博客,我遇到了 404 错误。
这是完整的错误信息:
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
admin/
register/ [name='register']
[name='blog-home']
about/ [name='blog-about']
The current path, register/POST, didn't match any of these.
这是主项目文件夹中的urls.py文件夹: 下面的 'users' 和 'views' 红色下划线
from django.contrib import admin
from django.urls import path, include
from *users* import *views* as user_views
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name='register'),
path('', include('blog.urls')),
]
这是我的 views.py,在我的 users 文件夹中:
从django.shortcuts导入渲染,重定向 从 django.contrib.auth.forms 导入 UserCreationForm from django.contrib import messages # 如果表单数据正确则显示消息
def register(request):
if request.method == 'POST': # if the request is a 'post' then it will create a form
form = UserCreationForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
messages.success(request, f'Account created for {username}!')
return redirect('blog-home')
else:
form = UserCreationForm() # anything not a post request it will create a blank form
return render(request, 'users/register.html', {'form': form})
以及 templates 内 users 目录中的 html 文件 register ]文件夹:
{% extends "blog/base.html" %}
{% block content %}
<div class=content-section">
<form action="Post">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today!!!</legend>
{{ form }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Sign Up</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">Already have an account? <a class="ml-2" href="#"></a>
</small>
</div>
</div>
{% endblock content %}
这是博客目录中的admin.py:
from django.contrib import admin
from . models import Post
admin.site.register(Post)
这是结构:
C:.
├───blog
│ ├───migrations
│ │ └───__pycache__
│ ├───static
│ │ └───blog
│ ├───templates
│ │ └───blog
│ └───__pycache__
├───django_project
│ └───__pycache__
└───users
├───migrations
│ └───__pycache__
├───templates
│ └───users
└───__pycache__
我已经尝试编辑代码等但没有发生变化。 如果您需要更多代码,请告诉我 请帮忙,谢谢
问题是您的表单有 action="Post"
,这导致浏览器访问不存在的 URL。
<form action="Post">
您应该使用 method="post"
让浏览器使用 POST 表单请求。
<form method="post">