I have a modelform in which I want to set required attribute to True for email validation
field:-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']
I tried
self.fields['email'].required=True
this resulted in class RegisterMyBuisinessForm doesnot have fields error
In order to make the summary field required, we need to create a custom form for the Post model. I am making them on the same file you can do this on a separate forms.py file as well. As intended we create the custom model form and in the __init__ method we made the summary field required.
Installation of django-crispy-forms is quite standard. You will need to use a package manager "pip" or "easy_install", then add an installed application 'crispy_forms' to the INSTALLED_APPS list. If you plan on using Bootstrap (or any other CSS framework), then you will also have to import it to the project.
FormHelper(form=None)[source] This class controls the form rendering behavior of the form passed to the {% crispy %} tag. For doing so you will need to set its attributes and pass the corresponding helper object to the tag: {% crispy form form.
You can alter self.fields['email']
in the __init__
method. You need to call super()
first.
class RegisterMyBuisinessForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
...
super(RegisterMyBuisinessForm, self).__init__(*args, **kwargs)
self.fields['email'].required = True
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With