我正在尝试使用django创建一个博客应用程序。当我访问本地主机时,主页工作正常,当我单击注册选项卡时,注册页面就会出现,当我单击提交时,它不会将数据提交到数据库中。返回未找到页面错误。请求方法:POST。请求网址:
http://127.0.0.1:8000/blog/register/register.I认为问题出在
网址。
html代码
{% extends 'layout.html' %}
{% block content %}
<div class="box">
<h2>
<center>Register</center>
</h2><br>
<form action='register' method='POST'>
{% csrf_token %}
<label><b>Email:</b></label><br>
<input type="email" class="inputvalues" name="email"/><br>
<label><b>Username:</b></label><br>
<input type="text" maxlength="100" class="inputvalues"
name="User_name"/><br>
<label><b>Password:</b></label><br>
<input type="password" class="inputvalues" name="Paasword1"/><br>
<label><b>Confirm Password:</b></label><br>
<input type="password" class="inputvalues" name="Paasword2"/><br>
<input type="Submit" id="lg"/><br>
<center><a href="#" >Already have an account.Login here.</a>
</center>
</form>
</div>
{% endblock %}
我的主要应用urls.py代码
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('blog/',include('blog.urls')),
path('admin/', admin.site.urls),
]
博客应用urls.py
from django.urls import path,include
from . import views
urlpatterns=[
path('',views.homepage),
path('register/',views.register,name='register'),
path('login/',views.login),
]
views.py注册页面代码
def register(request):
if request.method == "POST":
email=request.POST['email']
User_name=request.POST['User_name']
Password1=request.POST['Password1']
Password2=request.POST['Password2']
if Password1 == Password2:
if User.objects.filter(username=username).exists():
print('13')
elif User.objects.filter(email=email).exists():
print("email taken")
else:
user =
User.objects.create_user(username=User_name,
password=Password1,email=email)
user.save();
else:
return render(request,'register.html')
<!----------layout.html-->
{% load static %}
<html>
<head>
<title>Self Learn</title>
<link rel="stylesheet" type="text/css" href="{% static 'styles/main.css'
%}" />
</head>
<body>
<div class="header">
<h1 class="logo">Blog</h1>
<ul class="menu">
<a href="#">Home</a>
<a href="#">About Us</a>
<a href="login/">Login</a>
<a href='register/'>Register</a>
</ul>
</div>
<a href="http://www.google.com">
<img src="{% static 'images\12.PNG' %}" class="logo1"/>
</a>
<!----Post------>
{% for blog in blog %}
<div class="list">
<div class="con">
<h3 style="color:DodgerBlue;">{{blog.author}}</h3>
<h6 style="font-family: montserrat,sans-serif;"> On 22</h6>
</div>
<div class="line"></div>
<div class="con">
<h1><b>{{blog.title}}</b></h1>
</div>
<div class="con">
<p>{{blog.desc}}</p>
</div>
</div>
{% endfor %}
{% block content %}
{% endblock %}
</body>
</html>
当我单击register选项卡时,它将立即返回register.html文件 找不到URL http://12e时发生错误,此时URL为 http://127.0.0.1:8000/blog/register/register
答案 0 :(得分:0)
您在HTML上错误地使用了URL。您应该这样做:
<form action="{% 'register' %}" method='POST'>
....
</form>
答案 1 :(得分:0)
这应该工作
form action="{% url 'register' %}" method="POST">
在HTML文件中始终使用“”,而不是'
答案 2 :(得分:0)
我找到了解决方案。问题不是我的ide安装了python扩展。安装成功后。