Django:使用 cookie 保存和恢复表单状态
我想制作一个表单,一旦提交,在下次为同一用户渲染表单时,该表单将已经填写用户上次提交时的数据。听起来 django 应该有快捷方式 - 它有吗?如果不是,最简单的方法是什么?
我能想到的最好的办法是pickeling cleaned_data
并将其保存为cookie,稍后从中重新创建表单。
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?
The best I could come up with is pickeling cleaned_data
and saving it as a cookie, recreating the form from it later.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来已经是一个不错的计划了。但如果没有使用 cookie 的压力,我会改用会话。这样您就不会达到 cookie 的大小限制,并且传输较大大小的 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.
作为对此的扩展,我发现了一种使用会话通过基于类的视图保存表单数据的方法:
As an extension to this, here is a way I have found to use sessions to save the form data with a class-based view: