POST HTML FORM 验证不适用于 Django



我目前正在学习Django,我用我的views.py连接了我的注册表单,并写了一点后端代码,问题是它与我的。html文件成功链接,并且POST元素正在注册,但当我试图进行验证时,(if POST_element < 10),它什么也不做。

编辑:Post元素被我的后端识别,我用print(username)语句测试它,它工作得很好。

这是我的HTML注册表单的一部分:

<div class="limiter">
<div class="container-login100" style="background:black;">
<div class="wrap-login100">
<form action="{% url 'register' %}" method="POST" class="login100-form validate-form">
{% csrf_token %}
<span class="login100-form-logo">
<i class="zmdi zmdi-landscape"></i>
</span>
<span class="login100-form-title p-b-34 p-t-27">
Register
</span>
<div class="wrap-input100 validate-input" data-validate = "Enter username">
<input class="input100" type="text" name="username" placeholder="Username">
<span class="focus-input100" data-placeholder="&#xf207;"></span>
</div>

这是我的views.py管理寄存器的一部分:

def register(request):
if request.method=="POST":
username = request.POST["username"]
password = request.POST["pass"]
password_confirm = request.POST["pass-confirm"]
email = request.POST["mail"]

if len(username) < 7:
messages.error(request,"Username must be more than 10 char.") #Don't get any error
else:
messages.success(request,"Success")
return render(request, 'users/register.html')

这是我的url .py:

urlpatterns = [
path('', views.register, name='register'),
path('login/', views.login, name='login')
]

django-doc关于POST请求的摘录

在成功处理POST数据后,您应该始终返回HttpResponseRedirect。这个技巧不是Django特有的;这是一个很好的web开发实践。

您在POST请求中没有返回任何响应,并且messages框架是否有包含error,success,etc的消息。将消息发送到下一个重定向页面,所以这就是为什么你不能看到消息,你的条件是有效的。

检查以下代码:

Html代码:

<div class="limiter">
<div class="container-login100" style="background:black;">
<div class="wrap-login100">
<form class="login100-form validate-form"  method='POST' action="{% url 'users:register' %}" >
{% csrf_token %}
<span class="login100-form-logo">
<i class="zmdi zmdi-landscape"></i>
</span>
<span class="login100-form-title p-b-34 p-t-27">
Register
</span>
<div class="wrap-input100 validate-input" data-validate = "Enter username">
<input class="input100" type="text" name="username" placeholder="Username" required>
<span class="focus-input100" data-placeholder="&#xf207;"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter password">
<input class="input100" type="password" name="pass" placeholder="Password" required>
<span class="focus-input100" data-placeholder="&#xf191;"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Confirm password">
<input class="input100" type="password" name="pass-confirm" placeholder="Confirm Password" required>
<span class="focus-input100" data-placeholder="&#xf191;"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter Email">
<input class="input100" type="email" name="mail" placeholder="E-Mail" required>
<span class="focus-input100" data-placeholder="&#xf191;"></span>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn">
Register
</button>
</div>
<div class="text-center p-t-90">
<a class="txt1" href="login">
Already registered?
</a>
</div>
</form>
</div>
</div>
</div>

<style>标签的页面顶部设置以下样式,即内联样式:

<style>
.green{
color:green;
font-size:1.3rem;
}
.red{
color:red;
font-size:1.3rem;
}
</style>

urls . py

app_name='users'
urlpatterns = [
path('', views.register, name='register'),
path('login/', views.login, name='login')
]

Note:给出app_name是一个很好的做法,像template namespacing一样工作,同时给出url。

views.py

from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.contrib import messages
from django.urls import reverse

def register(request):
if request.method == "POST":
username = request.POST["username"]
password = request.POST["pass"]
password_confirm = request.POST["pass-confirm"]
email = request.POST["mail"]
# Here everying exist.
print("UserName : ", username)
print('Email : ', email)
print('Password : ', password)
print('Password Confirm : ', password_confirm)
if len(username) < 7:
# Here error exist.
print('Username must be more than 10 char.')
messages.error(
request, "Username must be more than 10 char.", 'red')
return HttpResponseRedirect(reverse('users:register'))
else:
messages.success(request, "Success! form submitted.", 'green')
return HttpResponseRedirect(reverse('users:register'))
return render(request, 'users/register.html')

Note:您也可以设置hashingmake_password的密码,即from django.contrib.auth.hashers import make_password

相关内容

  • 没有找到相关文章

最新更新