django的from组件
1.后端
from django import froms from django import forms from app import models class RegForm(forms.Form): username=forms.CharField(max_length=8,min_length=3,label='用户名', error_messages={ 'max_length':'用户名最长八位', 'min_length':'用户名最短三位', 'required':'用户名不能为空', },widget=forms.widgets.TextInput(attrs={'class':'form-control'})) password=forms.CharField(max_length=8,min_length=3,label="密码", error_messages={ 'max_length':'密码最长八位', 'min_length':'密码最短三位', 'required':'密码不能为空' },widget=forms.widgets.PasswordInput(attrs={'class':'form-control'})) confirm_password = forms.CharField(max_length=8, min_length=3, label="确认密码", error_messages={ 'max_length': '密码最长八位', 'min_length': '密码最短三位', 'required': '密码不能为空' }, widget=forms.widgets.PasswordInput(attrs={'class':'form-control'})) email=forms.EmailField(label='邮箱',error_messages={ 'lnvalid':'邮箱格式错误', 'required':'邮箱不能为空', },widget=forms.widgets.EmailInput(attrs={'class':'form-control'})) def clean_username(self): username=self.cleaned_data.get('username') user_obj=models.Userinfo.objects.filter(username=username).first() if user_obj: self.add_error('username','用户名已存在') else: return username def clean(self): password=self.cleaned_data.get('password') confir_password=self.cleaned_data.get('confirm_password') if not password == confir_password: self.add_error('confirm_password','两次密码不一致') else: return self.cleaned_data
2 前端渲染
{% for foo in form_obj%} <div class="form-group"> <label for="{{ foo.auto_id }}">{{ foo.label }}</label> {{ foo }} <span></span> </div> {% endfor %}