python - Pass values into Django form before form is made -


in forms.py file, let's have this...

class exampleform(forms.modelform):   class model:     model = example     fields = ['date_field']     widgets = {       'date_field' : selectdatewidget(years=year_range(some_variable))     }     exclude = () 

if wanted put some_variable while form being created , instance (form = exampleform(request.post or none, instance=instance_variable)), how 1 set value some_variable in form?

you should in form's constructor:

class exampleform(forms.modelform):     ...     def __init__(self, *args, **kwargs):         some_variable = kwargs.pop('some_variable')         super(exampleform, self).__init__(*args, **kwargs)         self.fields['date_field'].widget = selectdatewidget(                                                  years=year_range(some_variable)) 

and create form this:

form = exampleform(request.post or none,                    instance=instance_variable,                    some_variable=2015) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -