gpt4 book ai didi

python - Django Crispy 表单根据需要设置模型字段

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:46 33 4
gpt4 key购买 nike

我有一个模型表单,我想在其中将 required 属性设置为 True 以进行电子邮件验证

字段:-email

class RegisterMyBuisinessForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.form_action = '/registermybuisness/'
Field('email', type='email')
self.helper.add_input(Submit('submit', 'Submit',css_class="btn c-theme-btn c-btn-square c-btn-bold c-btn-uppercase"))
super(RegisterMyBuisinessForm, self).__init__(*args, **kwargs)
class Meta:
model = RegistermyBusiness
fields = ['name','email', 'org_name', 'contact','business_description','store_address','message','store_landmark','business_type','city']

我试过了

self.fields['email'].required=True 

这导致类 RegisterMyBuisinessForm 没有字段错误

最佳答案

您可以在 __init__ 方法中更改 self.fields['email']。您需要先调用 super()

class RegisterMyBuisinessForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
...
super(RegisterMyBuisinessForm, self).__init__(*args, **kwargs)
self.fields['email'].required = True
...

关于python - Django Crispy 表单根据需要设置模型字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47140951/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com