Reputation: 12645
I have a Django class-based-view for form-submission called myView as shown below. This view takes arguments submitted as part of the URL as well as data that the user fills into form fields. When the form submission is valid, everything works perfectly.
However, in the case that the form fields fail validation, then I want those errors to show up on the page and the user has to re-submit. The added wrinkle is that two extra arguments (myArg1 and myArg2) need to be sent as part of the context dictionary when this happens.
Currently, form_invalid returns super(myView, self).form_invalid(form)
. How do I add myArg1 and myArg2 to this return statement such that they are in the new re-submitted context?
class myView(FormView, TemplateView):
template_name = 'myTemplate.html'
form_class = myForm
success_url = reverse_lazy("mySuccess")
def form_valid(self, form):
BlahBlahBlah
def form_invalid(self, form):
myArg1 = "A"
myArg2 = "B"
return super(myView, self).form_invalid(form)
# I need to send back myArg1 and myArg2 as members of the
# context dictionary in the line above. How?
Upvotes: 0
Views: 1475
Reputation: 7460
I changed you code to follow a bit the python guidelines (regarding naming classes etc), what you need is in the form_invalid method.
Please don't use FormView
and TemplateView
together, there is no need...FormView
is enough:
class MyView(FormView):
template_name = 'my_template.html'
form_class = MyForm
success_url = reverse_lazy("my-success")
def form_invalid(self, form):
return self.render_to_response(self.get_context_data(form=form,myArg1='A',myArg2='B'))
Upvotes: 2