问题
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