Django:使用Cookie保存和恢复表单的状态 [英] Django: Save and restore a form's state using a cookie

查看:183
本文介绍了Django:使用Cookie保存和恢复表单的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一次提交的表单,对于同一用户的每次下一次表单呈现,表单都将填充上次用户提交的数据。听起来像django应该有快捷方式的东西 - 它有吗?

I want to make a form that once submitted, on each next rendering of the form for the same user, the form will be already filled out with the data from the last time the user submitted it. sounds like something that django should have shortcut for - does it have it? if not what would be the easiest way to do it?

我可以想出的最好的方法是pickeling washed_data 如果不是最简单的方法,

The best I could come up with is pickeling cleaned_data and saving it as a cookie, recreating the form from it later.

推荐答案

这听起来像是一个好的计划。但是如果没有使用cookie的压力,我会诉诸使用会话。

This already sounds like a good plan. But if there is no pressure on using cookies, I would resort to using the session instead. That way you won't hit a size limit on cookies, and transmitting cookies with higher size can slow down your response time.

form = MyForm(initial=request.session.get('form_data'))
...
if form.is_valid():
    request.session['form_data'] = form.cleaned_data

这篇关于Django:使用Cookie保存和恢复表单的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆