Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Created June 19, 2014 20:35
Show Gist options
  • Save cansadadeserfeliz/ec0f82bb3d302961503d to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/ec0f82bb3d302961503d to your computer and use it in GitHub Desktop.
Django: pass a variable from a form view to a form
class MyForm(forms.ModelForm):
requested_asset = None
def __init__(self, *args, **kwargs):
other_variable = kwargs.pop('other_variable')
super(MyForm, self).__init__(*args, **kwargs)
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
@ivancarrancho
Copy link

(y)

@timothe-chaumont
Copy link

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