from django import forms from user.models import Student, Teacher # 以下代码来实现老师和学生的登录信息表单 class StuLoginForm(forms.Form): uid = forms.CharField(label='学号', max_length=10) password = forms.CharField(label='密码', max_length=30, widget=forms.PasswordInput) class TeaLoginForm(forms.Form): uid = forms.CharField(label='教职工号', max_length=10) password = forms.CharField(label='密码', max_length=30, widget=forms.PasswordInput) # 为学生和老师这两种model都添加下对应的视图类 class StuRegisterForm(forms.ModelForm): confirm_password = forms.CharField(label='确认密码', widget=forms.PasswordInput()) # 其中Meta是元数据类,用于去编辑设置一些更深层次的设置。 # 要使用一个模型来创建表单,则在Meta元数据类中指定对应的model属性 class Meta: model = Student fields = ('grade', 'name', 'password', 'confirm_password', 'gender', 'birthday', 'email', 'info') def clean(self): cleaned_data = super(StuRegisterForm, self).clean() password = cleaned_data.get('password') confirm_password = cleaned_data.get('confirm_password', 'Password does not match') # 验证密码 if confirm_password != password: self.add_error('confirm_password', 'Password does not match.') class TeaRegisterFrom(forms.ModelForm): confirm_password = forms.CharField(label='确认密码', widget=forms.PasswordInput()) class Meta: model = Teacher fields = ('name', 'password', 'confirm_password', 'gender', 'birthday', 'email', 'info') def clean(self): cleaned_data = super(TeaRegisterFrom, self).clean() password = cleaned_data.get('password') confirm_password = cleaned_data.get('confirm_password', 'Password does not match') if confirm_password != password: self.add_error('confirm_password', 'Password does not match.') # 修改学生和老师的表单 class StuUpdateForm(StuRegisterForm): class Meta: model = Student fields = ('grade', 'name', 'password', 'gender', 'birthday', 'email', 'info') class TeaUpdateForm(StuRegisterForm): class Meta: model = Teacher fields = ('name', 'password', 'gender', 'birthday', 'email', 'info')