My question involves passing variables from the template to view in Django.
I know of passing variables in the URL and through a form. The problem I have with the first one is that the url could be manipulated which is not what I want. Is there anyway to prevent that?
Right now this is what I have as a band-aid:
<form action="/match/" method="post">
{% csrf_token %}
<input type="hidden" name="name1" value="{{ male_results }}">
<input type="hidden" name="userid1" value="{{ male_pic_userid }}">
<input type="hidden" name="name2" value="{{ female_results }}">
<input type="hidden" name="userid2" value="{{ female_pic_userid }}">
<input type="submit" value="Submit" />
</form>
Is there a way to avoid having to use this? Thank you!
There are broadly 3 ways to hold onto this kind of information:
Session (my suggestion for your situation)
Just stuff the data you want into the
request.sessiondictionary; it’ll persist per-user, and you can access it easily:Advantages
Disadvantages
POST, page content is dictated by the URL and session data — URLs are no longer unique, and users can’t share a particular page that relies on session infoQuery parameters
Something like
/match/?name1=foo1&userid1&name2=bar&userid2=2. You can either add these manually (<a href='/match/?name1={{ male_results }}...) or by changing yourPOSTform toGET.Advantages
Disadvantages
POST form (your current approach)
Advantages
Disdvantages
POSTdata to every navigation action is a huge pain.