Django: How to trigger a session 'save' of

2019-08-22 15:03发布

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条回答
女痞
2楼-- · 2019-08-22 15:56

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.

查看更多
登录 后发表回答