Skip to main content

Posts

Showing posts from April, 2012

Passing values from the view to the form.

Passing values from the view to the form it is really popular. Ex: form should be different for different kind of the user. Unfortunately I have not seen a documentation how to do this with class based view.


This task is separated in the two parts:
1) Prepare values in the view

class Myview():
""" This is only example view """
# set form
form_class = myformwithadditionallogic
# our function which pass some values to the form
def get_initial(self):
""" Passing initial values to the form"""
# default initial should be saved
super(Myview, self).get_initial()
self.initial = {"ourvalue":1}
return self.initial


That all for view.
Second part is retreive this data in the form:

class myformwithadditionallogic():
""" the example form . has no field only constructor
"""
def __init__(self, *args, **kwargs):
""" Constructor """
myvalues = kwargs.pop( 'initial', None)
#…