Tuesday 15 April 2014

python - Is it possible to specify specific form fields when initialising a form in a view? -


I have a form that I want to reuse around a site, but there are times where I Want to leave a specific area from the form? Is it possible to do this when starting this scene?

You can not delete a field, but you can form the form of the form as the __ Init __ can override the method. Django import forms class MyForm (forms.Form) from

 : some_field = forms .CharField () other_field = forms.CharField () def __init __ (self, my_criteria, * args, ** kwrds ): Super (MyForm, self) .__ init __ (* args, ** kwrds) if my_criteria == 'this': Self.fields ['some_field']. Widget = forms.HiddenInput (required = false) elif my_criteria == 'that': self.fields ['other_field']. Widget = forms.HiddenInput (required = false) #se: pass - Leave it as it is.   

If you want to present different types of forms, you can pass a parameter in one parameter while declaring the form, and on it HiddenInput Can use Runtime to render the widget which will hide it from

No comments:

Post a Comment