user7120561
user7120561

Reputation:

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

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?

Upvotes: 0

Views: 1114

Answers (1)

Giorgi Jambazishvili
Giorgi Jambazishvili

Reputation: 743

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.

Upvotes: 0

Related Questions