Django
的form
表单django
中的表单不是html
中的那个表单.这个表单是用来验证数据的合法性的一个东西,也可以生成HTML代码.
forms.py
的文件,放在指定的app当中,然后在里面写表单.forms.Form
,然后在里面定义要验证的字段.null=True
或者blank=True
等这几种参数了,有的参数是required=True/False
.is_valid()
方法可以验证用户提交的数据是否合法,而且HTML表单元素的name
必须和django
中的表单的name
保持一致,否则匹配不到.is_bound
属性:用来表示form
是否绑定了数据,如果绑定了,则返回True
,否则返回False
.cleaned_data
:这个是在is_valid()
返回True
的时候,保存用户提交上来的数据.#-------------form.py------------
class RegisterForm(forms.Form):
username = forms.CharField(max_length=8,min_length=6)
password = forms.CharField(max_length=8,min_length=6,
widget=forms.PasswordInput(attrs={'placeholder':'请输入密码'}),
error_messages={'min_length':'密码长度小于6',
'max_length':'密码长度超过8了'})
password_repeat = forms.CharField(max_length=8,#min_length=6,
widget=forms.PasswordInput(attrs={'placeholder':'请再输入密码'}),
error_messages = {'min_length': '密码长度小于6'}
)
email = forms.EmailField()
这些参数会对页面的输入做一些限制条件:
max_length 最大长度
min_length 最小长度
widget 负责渲染网页上HTML 表单的输入元素和提取提交的原始数据
attrs 包含渲染后的Widget 将要设置的HTML 属性
error_messages 报错信息
# form.py
class LoginForm(forms.Form):
username = forms.CharField(label='用户名')
password = forms.CharField(label='密码',
widget = forms.PasswordInput(attrs={'placeholder': '请输入密码 '}))
class RegisterForm(forms.Form):
username = forms.CharField(max_length=8,min_length=6)
password = forms.CharField(max_length=8 ,#min_length=6,
widget=forms.PasswordInput(attrs={'placeholder':'请输入密码'}))
password_repeat = forms.CharField(max_length=8,#min_length=6,
widget=forms.PasswordInput(attrs={'placeholder':'请再输入密码'}))
email = forms.EmailField()
# ------urls.py-------
from django.conf.urls import url
from . import views
urlpatterns = [
path('home/',views.home,name='ts22_home'),
path('login/',views.login,name='ts22_login'),
path('logout/',views.logout,name='ts22_logout'),
path('register/',views.register,name='ts22_register'),
]
#---------home.py------
欢迎 {{ username }}
登录
注册
退出
# --------login.html--------
# --------register.html------
# ----------views.py---------------
from django.shortcuts import render,reverse
from django.http import HttpResponse,HttpResponseRedirect
# Create your views here.
def home(request):
username = request.session.get('username','未登录')
return render(request,'ts22/home.html',
{'username':username})
from .models import UserModel
from .form import LoginForm,RegisterForm
def login(request):
if request.method == 'GET':
userForm = LoginForm()
return render(request,'ts22/login.html',
{'userForm':userForm})
elif request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
userModel = UserModel.objects.filter(username=username,password=password)
if userModel:
# response = HttpResponseRedirect(reverse('ts11_home'))
# response.set_cookie('usename',username)
request.session['username']=username
# request.session.set_expiry(180)
return HttpResponseRedirect(reverse('ts22_home'))
else:
return HttpResponseRedirect(reverse('ts22_register'))
def logout(request):
request.session.flush()
return HttpResponseRedirect(reverse('ts22_home'))
def register(request):
if request.method == 'GET':
form = RegisterForm()
return render(request,'ts22/login.html',
{'userForm':form})
elif request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username', None)
password = form.cleaned_data.get('password', None)
password_repeat = form.cleaned_data.get('password_repeat', None)
email = form.cleaned_data.get('email', None)
if password == password_repeat:
user = UserModel()
user.username = username
user.password = password
user.email = email
user.save()
return HttpResponse('注册成功')
else:
return HttpResponse('注册失败')