模型model:定义用户类,用来注册时使用
from django.db import models
# Create your models here.
class Person(models.Model):
gender=(
('male','男'),
('female','女'),
)
username=models.CharField(max_length=128)
password=models.CharField(max_length=128)
sex=models.CharField(choices=gender,max_length=32,default='男')
email=models.EmailField(max_length=32)
#秒数过于精确,指定该格式
register_time=models.DateTimeField(default=date.today().strftime('%Y-%m-%d %H:%M:%S'))
def __str__(self):
return self.username
def Meta(self):
ordering=['-username']
verbose_name="用户"
verbose_name_plural="用户"
表单forms:模型与html网页沟通的桥梁,表单获取网页数据【<td>{{ form.username.label_tag }}</td>】,再传入模型中【person_model=模型.objects.create(**表单)】。模型中的一般字段都应该出现在forms表单,表单还可定义一些其他的字段,并做一些校验,渲染错误等操作(浏览器渲染采用循环表单进行渲染,html中并不需要输入创建时间,所以表单中没有时间字段,但模型中包含创建时间,根据自己的需要合理安排),表单中的error_messages中各属性对于前面forms.CharField中的各属性一致,比如max_length,若max_length不满足,则error_messages会得到后面的消息提示。
from django import forms
from django.core.exceptions import ValidationError
class register_form(forms.Form):
gender = (
('male', '男'),
('female', '女'),
)
username = forms.CharField(label='username',max_length=20,min_length=6,error_messages={'required':"用户名不能为空",'max_length':"用户名长度应小于20位",'min_length':"用户名长度应大于6位"})
password=forms.CharField(label='password',widget=forms.PasswordInput,max_length=20,min_length=6,error_messages={'required':"密码不能为空",'min_length':"密码应大于6位",'max_length':"密码应小于20位"})
submit_password=forms.CharField(label='submit_password',widget=forms.PasswordInput(attrs={'class':'form-control'}),error_messages={'required':"密码不能为空"})
email=forms.EmailField(label='email')
sex=forms.ChoiceField(widget=forms.RadioSelect,choices=gender)
def clean(self):
pa=self.cleaned_data.get('password')
submit_pa=self.cleaned_data.get('submit_password')
if pa!=submit_pa:
self.add_error('submit_password',ValidationError("两次密码不一致"))
else:
return self.cleaned_data
HTML:没搞明白html中为啥没import,仅仅用form就使用了我forms中的register_form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<h1>注册界面</h1>
<form method="post">
{% csrf_token %}
<table>
<tr><td>{{ form.username.label_tag }}</td><td>{{ form.username }}</td></tr>
<tr><td>{{ errors.username }}</td></tr>
<tr><td>{{ form.password.label_tag }}</td><td>{{ form.password }}</td></tr>
<tr><td>{{ errors.password }}</td></tr>
<tr><td>{{ form.submit_password.label_tag }}</td><td>{{ form.submit_password }}</td></tr>
<tr><td>{{ errors.submit_password }}</td></tr>
<tr><td>{{ form.email.label_tag }}</td><td>{{ form.email }}</td></tr>
<tr><td>{{ errors.email }}</td></tr>
<tr><td>{{ form.sex.label_tag }}</td><td>{{ form.sex }}</td></tr>
<tr><td>{{ errors.sex }}</td></tr>
<tr>
<td><input type="reset" value="重置"></td>
<td><input type="submit" value="提交"></td>
<td><input type="button" value="主页" onclick="window.location.href='http://127.0.0.1:8000/login_register/login/'"></td>
</tr>
</table>
</form>
</body>
</html>
https://www.cnblogs.com/zaizai1573/p/10512114.html
https://www.cnblogs.com/WiseAdministrator/articles/10975786.html