我有一个类QuestionView
,它派生自FormView
类。这里
是一个解释我的问题的代码片段:
class QuestionView(FormView):
...
context_var1 = y
def form_valid (self, form):
...
self.context_var1 = x
...
def get_context_data(self, **kwargs):
...
context['context_var1'] = self.context_var1
...
return context
如上所示,我在form_valid
和更新中更新了一组上下文变量
我打算在模板中使用这些更新的值 - 因此context
字典中的变量。这段代码的问题在于改变了
未见context_var1
- 可能是因为get_context_data
在form_valid
方法之前调用。是否有解决方法
此?
答案 0 :(得分:25)
我使用form_invalid
执行此操作。我是这样做的:
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)
你可以做同样的事情,但是对于form_valid。通常,form_valid的主体如下所示:
def form_valid(self, form):
return HttpResponseRedirect(self.get_success_url())
您必须同时覆盖post
和form_valid
,因为post
会调用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())
哦,只是为了澄清,存在此问题的原因是ProcessFormView
类的get
方法被破坏了。通常看起来像这样:
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))
它只是把kwargs扔掉了(._.)
答案 1 :(得分:5)
在Django 2.0.1中,您可以通过覆盖Message
或get_context_data
来插入上下文数据。
在您的情况下,您可以执行以下某项覆盖:
form_invalid
或者:
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
)
)
Django 2.0.1将表单插入到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)
的kwargs中。
get_context_data
答案 2 :(得分:1)
在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
在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>
我只是在Django 1.10.3中找到了这个方法。 希望可以帮到你
答案 3 :(得分:0)
也许你可以使用这种方法:
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)