最近在学习Django的过程中,发现Django里面对表单的支持非常棒,简化了很多操作,例如可以很方便的生成Html表单,对表单进行验证以及最终的数据存储等等,简单的一个实例如下:
class AccountForm(forms.ModelForm):
nick_name = forms.CharField(label=u'用户昵称', \
help_text="请输入用户昵称")
email = forms.EmailField(label=u'电子邮件', \
help_text=u'请输入常用邮箱')
password = forms.CharField(label=u'密码', \
help_text=u'请输入密码', \
widget=forms.PasswordInput())
re_password = forms.CharField(label=u'确认密码', \
help_text=u'请再次确认密码', \
widget=forms.PasswordInput())
def clean_re_password(self):
password = self.cleaned_data.get("password")
re_password = self.cleaned_data.get("re_password")
if password and re_password and password != re_password:
raise forms.ValidationError((u'密码输入不一致'))
if len(password) < 6:
raise forms.ValidationError((u'密码至少6位'))
return re_password
class Meta:
model = Account
fields = ('nick_name', 'email', 'password')
在模板中只需要简单的添加
这样就完成了简单的表单设计。
Django的这种方法确实很简单,但是Django固有的表单样式比较单一,虽然可以通过widget属性添加css样式,但是明显DIY程度不够高,本着前后端分离的原则,需要将表单的样式彻底分离出来,做到完全定制。
具体的表单自定义方式实际在Django的官方文档里面也有提及
Useful attributes on {{ field }} include:
{{ field.label }}
The label of the field, e.g. Email address.
{{ field.label_tag }}
The field’s label wrapped in the appropriate HTML
q
q
Cytogenetics. Ylhtao https://oscialipop.com - buying cialis online reviews <a href=https://oscialipop.com>Cialis</a> Mophwh How To Purchase Doxycycline Vhhrjf Achetez Priligy 30 Mg Entrega Rapida https://oscialipop.com - Cialis Can You Take Amoxicillin While Pregnant