Python中文
首页
教程
问答
标签
搜索
登录
注册
在Djang中重用表单域
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>有人问这个问题:<a href="https://stackoverflow.com/questions/5184503/django-reuse-form-fields-without-inheriting">Django: Reuse form fields without inheriting?</a></p> <p>虽然,公认的答案是明确的,但它不是很方便,如果我想重写许多表单方法。在</p> <p>大多数投票的答案有点困难,而且不起作用。在</p> <p>那么,在继承或不继承的情况下,将相同的字段包含到多个表单(表单或ModelForms)中的清晰而python方法是什么呢?在</p> <p>例如,我希望下面的类是可重用的。在</p> <pre><code>class SetPasswordMixin(forms.Form): password1 = forms.CharField(label=_('Password'), widget=forms.PasswordInput(attrs={'placeholder': _('Password')})) password2 = forms.CharField(label=_('Password confirmation'), widget=forms.PasswordInput(attrs={'placeholder': _('Password confirmation')})) def clean_password2(self): # Check that the two password entries match password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise forms.ValidationError(_("Passwords don't match")) return password2 </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>可以使用多重继承组合两个窗体:</p> <pre><code>class SetPasswordMixin(forms.Form): ... class MessageFormBase(forms.ModelForm): class Meta: model = Message class MessageForm(MessageFormBase, SetPasswordMixin): pass </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何提高Djang的410误差
7 回答
如何提高doc2vec模型中两个文档(句子)的余弦相似度?
6 回答
如何提高Docker的日志限制?|[输出已剪裁,达到日志限制100KiB/s]
3 回答
如何提高DQN的性能?
10 回答
如何提高EasyOCR的准确性/预测?
9 回答
如何提高Euler#39项目解决方案的效率?
7 回答
如何提高F1成绩进行分类
5 回答
如何提高FaceNet的准确性
4 回答
如何提高fft处理的精度?
10 回答
如何提高Fibonacci实现对大n的精度?
3 回答
如何提高Flask与psycopg2的连接时间
3 回答
如何提高FosterCauer变换的scipy.signal.invres()的数值稳定性?
5 回答
如何提高gae查询的性能?
1 回答
如何提高GANs用于时间序列预测/异常检测的结果
7 回答
如何提高gevent和tornado组合的性能?
10 回答
如何提高googleappengin请求日志的吞吐量
9 回答
如何提高googlevision文本识别的准确性
8 回答
如何提高groupby/apply效率
1 回答
如何提高Gunicorn中的请求率
6 回答
如何提高G中的文件编码转换
5 回答