Django web开发之Form组件
Form组件使用
参照文档 https://docs.djangoproject.com/zh-hans/2.2/topics/forms/
生成HTML标签
创建一个form类
from django import forms
class RegForm(forms.Form):
name = forms.CharField(max_length=6, label='用户名')
pwd = forms.CharField(label='密码', min_length=8, widget=forms.widgets.PasswordInput(attrs={'class':'pwd'}))
# widget 插件,默认为select,这里修改为密码用的插件
"""
Django内置插件:
参照:https://docs.djangoproject.com/zh-hans/3.0/ref/forms/widgets/
TextInput(Input)
NumberInput(TextInput)
EmailInput(TextInput)
URLInput(TextInput)
PasswordInput(TextInput)
HiddenInput(TextInput)
Textarea(Widget)
DateInput(DateTimeBaseInput)
DateTimeInput(DateTimeBaseInput)
TimeInput(DateTimeBaseInput)
CheckboxInput
Select
NullBooleanSelect
SelectMultiple
RadioSelect
CheckboxSelectMultiple
FileInput
ClearableFileInput
MultipleHiddenInput
SplitDateTimeWidget
SplitHiddenDateTimeWidget
SelectDateWidget
"""
"""
"""
views
def reg(request):
form_ojb = RegForm()
return render(request, 'reg.html', {'form_obj':form_ojb})
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<h1>注册</h1>
<div>
<form action="/reg" novalidate>
{% csrf_token %}
<p> {{ form_obj.name.label }}{{ form_obj.name }} </p>
<p> {{ form_obj.pwd.label }} {{ form_obj.pwd}} <p>
<input type="submit" value="注册">
</form>
</div>
</body>
</html>
浏览器显示
查看下源码
<div>
<form action="/reg2" novalidate>
<input type="hidden" name="csrfmiddlewaretoken" value="dXc93xMHJX15g3L3NaazqN9WYEobMwFFWICodZQdHMEXK4E3GVAqQpleAqZBIZF2">
<p> 用户名<input type="text" name="name" maxlength="6" required id="id_name"> </p>
<p> 密码 <input type="password" name="pwd" class="pwd" minlength="8" required id="id_pwd"> <p>
<input type="submit" value="注册">
</form>
</div>
验证用户数据(显示错误信息)
修改form类
from django import forms
class RegForm(forms.Form):
name = forms.CharField(max_length=20, label='用户名')
pwd = forms.CharField(label='密码', max_length=16, min_length=8,
widget=forms.widgets.PasswordInput(attrs={'class':'pwd'}),
error_messages={'min_length':"密码太短了",
"max_length":'密码超过最大限制'})
# error_messages 用来自定义错误消息,如果不设置,采用默认的错误消息。
"""
Django 内置字段类型:
***参照文档 :https://docs.djangoproject.com/zh-hans/3.0/ref/forms/fields/***
views
def reg(request):
form_obj = RegForm()
if request.method == "POST":
form_obj = RegForm(request.POST)
if form_obj.is_valid(): # 校验数据,如果成功则进行下一步
if form_obj.is_valid():
values = form_obj.clean()
# form_obj.clean() 取得保存所有数据的字典,也可以使用form_obj.cleaned_data
print(values) # 这里可进行数据处理,保存到数据库中
else:
errors = form_obj.errors
print(errors)
return render(request, 'reg2.html', {'form_obj':form_obj})
修改函数reg,使得在页面打开时,初始化页面内容
def reg(request):
if request.method == "GET":
values = {'name':"张三", 'pwd':None}
form_obj = RegForm(values)
else request.method == "POST":
form_obj = RegForm(request.POST)
if form_obj.is_valid(): # 校验数据,如果成功则进行下一步
if form_obj.is_valid():
values = form_obj.clean()
# form_obj.clean() 取得保存所有数据的字典,也可以使用form_obj.cleaned_data
print(values) # 这里可进行数据处理,保存到数据库中
else:
errors = form_obj.errors
print(errors)
return render(request, 'reg2.html', {'form_obj':form_obj})