有这个代码:
@method_decorator(login_required, name='dispatch')
class MainFormView(DataMixin, FormView):
form_class = MainForm
template_name = 'sitee/index.html'
success_url = reverse_lazy('home')
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['menu_selected'] = 'home'
c_def = self.get_user_context(title="Главная")
return dict(list(context.items()) + list(c_def.items()))
def form_valid(self, form):
# print(POST)
#context['result'] = POST['title']
return redirect('home')
#index.html
<form method="post">
{% csrf_token %}
<div class="form-error">{{ form.non_field_errors }}</div>
<input id="title" name="title" type="text"/>
<input id="datetime" name="datetime" type="text"/>
<label for="id_groups_0">
<input type="checkbox" name="groups" value="0" id="id_groups_0">
Группа 2
</label>
<label for="id_groups_1">
<input type="checkbox" name="groups" value="0" id="id_groups_1">
Группа 1
</label>
<button type="submit">Отправить</button>
</form>
{{result}}
当您点击主页时,会出现此表单。点击“提交”后如何显示来自 post 请求的数据,将 post['title'] 中的值添加到上下文中并显示在页面上
通用视图具有方法
get
和post
. 双方都打电话self.get_context_data()
。您覆盖它,从中获取POST
请求数据self.request.POST
并做任何您想做的事情。原始示例
如果答案没有帮助,那么说明需要什么