User Register and Login
A-基本页面
-在usls。py中创建register和login的url
-在viesw中写入registerPage函数
-引入数据库USER中的Form-UserCreationForm
from django.contrib.auth.forms import UserCreationForm def registerPage(request): form = UserCreationForm() if request.method==‘POST‘: form = UserCreationForm(request.POST) if form.is_valid(): form.save() context = {‘form‘: form} return render(request,‘accounts/register.html‘,context)
-在template模板中写入register。html
-{{form_as.p}} 使其按顺序排列
-在forms.py中自定义model 引入User 选择属性
from django.forms import ModelForm from .models import Order from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User class OrderForm(ModelForm): class Meta: model = Order fields = ‘__all__‘ class CreatUserForm(UserCreationForm): class Meta: model = User fields = [‘username‘,‘email‘,‘password1‘,‘password2‘]
-更改有关views中的引入函数
from .forms import OrderForm,CreatUserForm form = CreatUserForm() form = CreatUserForm(request.POST)
-在r127.0.0.1/register 添加数据 从127.0.0.1/admin/User中查找
B 渲染模板
-在register。html中修改页面的渲染方式 单个单个渲染方便后面套用JS
<h3>Register</h3> <form method="POST" action=""> {% csrf_token %} {{ form.username.label }} {{ form.username }} {{ form.email.label }} {{ form.email }} {{ for.password1.label }} {{ form.password1 }} {{ form.password2.label }} {{ form.password2 }} <input type="submit" name="Create User"> </form>
-访问 jsfiddle 找到js 渲染模板
-注册表格模板: https://jsfiddle.net/ivanov11/hzf0jxLg/
-登录表单模板: https://jsfiddle.net/ivanov11/dghm5cu7/
原文:https://www.cnblogs.com/kangkang1999/p/13401523.html