POST HTML FORM 验证不适用于 Django
POST HTML FORM verificaton doesn't work with Django
我目前正在学习 Django,我将我的注册 FORM 与我的 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=""></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')
这是我的 urls.py:
urlpatterns = [
path('', views.register, name='register'),
path('login/', views.login, name='login')
]
关于 POST
请求的 django-doc
节选。
You should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good web development practice in general.
您在 POST
请求中没有 return 任何响应,并且 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=""></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=""></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=""></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=""></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:
Giving app_name
is a good practice,works like template namespacing
while giving urls.
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:
You can also do password hashing
through make_password
which is from django.contrib.auth.hashers import make_password
.
我目前正在学习 Django,我将我的注册 FORM 与我的 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=""></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')
这是我的 urls.py:
urlpatterns = [
path('', views.register, name='register'),
path('login/', views.login, name='login')
]
关于 POST
请求的 django-doc
节选。
You should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good web development practice in general.
您在 POST
请求中没有 return 任何响应,并且 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=""></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=""></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=""></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=""></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:
Givingapp_name
is a good practice,works liketemplate namespacing
while giving urls.
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:
You can also do passwordhashing
throughmake_password
which isfrom django.contrib.auth.hashers import make_password
.