Django: How to trigger a session 'save' of form data when clicking a non-submit link

我怕爱的太早我们不能终老 提交于 2020-01-06 05:08:06

问题


I know if want you to store form information to the session during a submit you do it with the 'def post' in your view. However, I can not figure out how to store form information when a random link e.g. 'homepage'.

How would you go about storing form information when a user clicks away from the form?


回答1:


To store information in the session, you don't really need to post, or submit some kind of form. You can do it anywhere, where you have request using session attribute.

the session is dict like object and you can save there any basic types (str, int, float) if you use django's basic configuration, like so:

request.session["data1"] = "my data stored in session"
request.session.get("data2")

also, please note that you may directly have no session accessible, since django won't automatically create session for you. in fact to resolve the issue you could "initialize" it, with: request.session.save()

please take a look at official documentation.



来源:https://stackoverflow.com/questions/47959416/django-how-to-trigger-a-session-save-of-form-data-when-clicking-a-non-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!