5👍
get_context_data
does not work with a request
parameter. You can pass an arbitrary number of parameters, but here your get_context_data
will only run if it is called with the request. You access the request with self.request
:
class RiskSet(SingleTableMixin, FormView):
template_name = "risk/RiskSet.html"
model = RiskSet
form_class = RiskSetForm
def get_context_data(self, *args, **kwargs):
form = RiskSetForm(self.request.POST or None)
if form.is_valid():
form.save()
# Call the base implementation first to get a context
context = super().get_context_data(*args, **kwargs)
# Add in a QuerySet of all the books
context['page'] = 'risk'
return context
Furthermore it makes no sense to do this in the get_context_data
method. A FormView
has routines in place for this. It You can probably also work with a CreateView
which will remove more boilerplate code, like:
from django.views.generic import CreateView
class RiskSet(SingleTableMixin, CreateView):
template_name = "risk/RiskSet.html"
model = RiskSet
form_class = RiskSetForm
success_url = 'path-to-url-when-form-is-valid'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
# Add in a QuerySet of all the books
context['page'] = 'risk'
return context
Source:stackexchange.com