django.contrib.auth.forms
1、class AuthenticationForm
用户登录的表单。
def index_login(request):
context = {}
if request.method == 'GET':
form = AuthenticationForm
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
login(request, form.get_user())
return redirect(to='list')
context['form'] = form
return render(request, 'register_login.html', context)
2、class UserCreationForm
建立新用户的 ModelForm 。
它有三个字段: username (来自用户模型),password1 ,和 password2 。它检查 password1 和 password2 是否匹配,使用 validate_password() 验证密码,并且使用 set_password() 来设置用户密码。
def index_register(request):
context = {}
if request.method == 'GET':
form = UserCreationForm
if request.method == 'POST':
form = UserCreationForm(request.POST)
print(form)
if form.is_valid():
form.save()
return redirect(to='login')
context['form'] = form
return render(request, 'register_login.html', context)