django的from组件
生活随笔
收集整理的這篇文章主要介紹了
django的from组件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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="確認(rèn)密碼",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 usernamedef 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
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="確認(rèn)密碼",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 usernamedef 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 %}
轉(zhuǎn)載于:https://www.cnblogs.com/xzcvblogs/p/11262171.html
總結(jié)
以上是生活随笔為你收集整理的django的from组件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pods私有库制作
- 下一篇: MongoDB 之 手把手教你增删改查