Created
June 19, 2014 20:35
-
-
Save cansadadeserfeliz/ec0f82bb3d302961503d to your computer and use it in GitHub Desktop.
Django: pass a variable from a form view to a form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyForm(forms.ModelForm): | |
requested_asset = None | |
def __init__(self, *args, **kwargs): | |
other_variable = kwargs.pop('other_variable') | |
super(MyForm, self).__init__(*args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCreateView(CreateView): | |
model = MyModel | |
template_name = 'myapp/my_form.html' | |
form_class = MyForm | |
other_variable = None | |
def get_form_kwargs(self): | |
kwargs = super(MyCreateView, self).get_form_kwargs() | |
kwargs.update({'other_variable': self.other_variable}) | |
return kwargs |
Thank you! This code is perfectly clear.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(y)