26π
I do this with form_invalid
. Hereβs how I do it:
from django.views.generic import FormView
class ContextFormView(FormView):
def get(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
context = self.get_context_data(**kwargs)
context['form'] = form
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form, **kwargs)
def form_invalid(self, form, **kwargs):
context = self.get_context_data(**kwargs)
context['form'] = form
return self.render_to_response(context)
You could do the same but for form_valid. Normally the body of form_valid looks like this:
def form_valid(self, form):
return HttpResponseRedirect(self.get_success_url())
You would have to override both post
and form_valid
, because post
calls form_valid
.
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form, **kwargs)
else:
return self.form_invalid(form, **kwargs)
def form_valid(self, form, **kwargs):
# take some other action here
return HttpResponseRedirect(self.get_success_url())
oh and just to clarify, the reason this problem exists is that the ProcessFormView
classβs get
method is broken. It normally looks like this:
def get(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
return self.render_to_response(self.get_context_data(form=form))
It just throws the kwargs away (._.)
5π
In views.py
class UploadTest(FormView):
template_name = 'test.html'
....
plus_context = dict()
def form_valid(self, form):
...
self.plus_context['you_want_context'] = value
...
return super(UploadTest, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(UploadTest, self).get_context_data(**kwargs)
context['plus_context_key'] = self.plus_context
return context
In test.html
<html>
....
<body>
....
// first post can not get plus_context_key
{% if plus_context_key %}
{{ plus_context_key.you_want_context }}
{% endif %}
</body>
</html>
I just figure out this way in Django 1.10.3.
Hope can help you
- Can I detect if my code is running on cPython or Jython?
- Foreign Key to User model
- Django version of flask.jsonify jsonify
5π
In Django 2.0.1 you can insert context data by overriding either get_context_data
or form_invalid
.
In your case you could do one of the following overrides:
class QuestionView(FormView):
...
def form_invalid(self, form):
"""If the form is invalid, render the invalid form."""
return self.render_to_response(
self.get_context_data(
form=form,
context_key=some_value
)
)
Or:
class QuestionView(FormView):
...
def get_context_data(self, **kwargs):
if 'context_key' not in kwargs: # set value if not present
kwargs['context_key'] = some_value
return super().get_context_data(**kwargs)
Django 2.0.1 inserts under the hood the form into the kwargs of get_context_data
.
# File: django.views.generic.edit
class FormMixin(ContextMixin):
...
def form_valid(self, form):
"""If the form is valid, redirect to the supplied URL."""
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form):
"""If the form is invalid, render the invalid form."""
return self.render_to_response(self.get_context_data(form=form))
def get_context_data(self, **kwargs):
"""Insert the form into the context dict."""
if 'form' not in kwargs:
kwargs['form'] = self.get_form()
return super().get_context_data(**kwargs)
- How to test django model method __str__()
- How to get user email with python social auth with facebook and save it
- Django Views: When is request.data a dict vs a QueryDict?
- What is Serializers to_internal_value method used for in Django
0π
Maybe you can use this approach:
class SomeView(View):
def post(self, request):
my_form = MyForm(request.POST)
if my_form.is_valid():
context['foo'] = 'bar'
return render(request, 'valid/path.html', context)
- Django InlineModelAdmin β set inline field from request on save (set user field automatically) (save_formset vs save_model)
- Is there a command for creating an app using cookiecutter-django?
- Django: Possible to load fixtures with date fields based on the current date?
0π
Pass plus_context just if save successfull:
class SomeUpdateView(UpdateView):
...
success_url = reverse_lazy('SomeUpdateView')
plus_context = dict()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.plus_context:
context['success'] = self.plus_context['pass_to_view_just_after_save_successful']
return context
def form_valid(self, form, **kwargs):
self.object.save()
self.plus_context['pass_to_view_just_after_save_successful'] = 'Save successful!'
return super(SomeUpdateView, self).form_valid(form)
in template something like:
{% if success %}
<p>{{ success }}</p>
{% endif %}
- Clean Up HTML in Python
- A good collaborative filtering/matching/recommendation library for Python/Django?
- What does deconstructible do in Python?
- Crispy-forms: add css class for one of the inputs
- Using existing field values in django update query