Django form

时间:2023-01-30 10:55:32

简单的from验证

文件目录结构

Django form

urls.py

from app1.views import account
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/',account.index),
]

  

settings.py

STATICFILES_DIRS =(
os.path.join(BASE_DIR,'static')
)
##配置静态路由

  

forms.py

from django import forms
from app1 import models class Form1(forms.Form):
user =forms.CharField(
# widget 自定义froms属性
widget=forms.TextInput(attrs={'class':'c1'}),
error_messages={'required':'用户名不可以为空'},
)
# max_length最长多少位,min_length最少多少位
pwd =forms.CharField(max_length=4,min_length=2)
# error_messages 错误信息 required 审核是否为空 invalid 格式是否正确
email =forms.EmailField(error_messages={'required':'邮件不可以为空',
'invalid':'邮箱格式错误',
})
memo =forms.CharField(
# Textarea文本下拉框
widget=forms.Textarea()
) user_type_choice=(
(0,'人体学'),
(1,'高级科技学'),
)
# user_type_choice = models.BookType.objects.values_list('id', 'caption') ##从数据库取数据 book_type =forms.CharField(
widget=forms.widgets.Select(choices=user_type_choice,
attrs={'class':"form-control"}))
# Select 下拉框属性 choices 选择的东西 ,attrs获取属性

  

views.account.py

from django.shortcuts import render

# Create your views here.
from app1.forms import Form1 def index(request):
if request.method=='POST':
f =Form1(request.POST)##判断用户输入的是否合法
if f.is_valid():##如果用户输入的合法
print(f.cleaned_data)
# cleaned_data中字段值是个model实例字典。 else:##不合法
print('&&&&&&&&&&&*********',f,'$$$$$$$$$$#########')
return render(request,'account/form1.html',{'error':f.errors,'form':f}) else:
f =Form1() ##空值,
return render(request,'account/form1.html',{'error':f.errors,'form':f})
F 里面输出 &&&&&&&&&&&********* <tr><th><label for="id_user">User:</label></th><td><ul class="errorlist"><li>用户名不可以为空</li></ul><input class="c1" id="id_user" name="user" type="text" required /></td></tr> <tr><th><label for="id_pwd">Pwd:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input id="id_pwd" maxlength="4" minlength="2" name="pwd" type="text" required /></td></tr> <tr><th><label for="id_email">Email:</label></th><td><ul class="errorlist"><li>邮件不可以为空</li></ul><input id="id_email" name="email" type="email" required /></td></tr> <tr><th><label for="id_memo">Memo:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><textarea cols="40" id="id_memo" name="memo" rows="10" required> </textarea></td></tr> <tr><th><label for="id_book_type">Book type:</label></th><td><select class="form-control" id="id_book_type" name="book_type" required> <option value="0" selected="selected">人体学</option> <option value="1">高级科技学</option> </select></td></tr> $$$$$$$$$$#########

  

form1.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/index/" method="post">
<div class="inline-group">
{{ form.user }}
{# form.user用户,error.user.0 判断有没有error信息 #}
{% if error.user.0 %}
<span>{{ error.user.0 }}</span>
{% endif %}
</div>
<div class="inline-group">
{{ form.pwd }}
{% if error.pwd.0 %}
<span>{{ error.pwd.0 }}</span>
{% endif %}
</div>
<div class="inline-group">
{{ form.email }}
{% if error.email.0 %}
<span>{{ error.email.0 }}</span>
{% endif %}
</div>
<div class="inline-group">
{{ form.memo }}
{% if error.memo.0 %}
<span>{{ error.memo.0 }}</span>
{% endif %}
</div>
<div class="inline-group">
{{ form.book_type }}
{% if error.book_type.0 %}
<span>{{ error.book_type.0 }}</span>
{% endif %}
</div>
<div>
<input type="submit" value="提交">
</div> </form> </body>
</html>

  

输出效果

Django form