I'm using Django and I am having trouble passing variable information back to the server without modifying the url. Here's what I have which works the way I want except that I want the action to be /foo:
<form method="post" action="/foo/{{ variable }}">
<input type="submit" value="bar"/>
</form>
When I do this I can easily get the variable when I parse the url. Instead, I want to add the variable to the POST QueryDict. Is this possible?
Yes. just add additional input fields:
<form method="post" action="/foo/">
<input type="hidden" value="{{ variable }}" name="name_of_var">
<input type="submit" value="bar"/>
</form>
Then you can access the variable in the view by:
request.POST['name_of_var']
or if you are not sure if the variable exists. This will not raise exception is does not exist.
request.POST.get('name_of_var')
Just make an input inside your form:
<input type="hidden" value="{{ variable }}" name="foo" />
Then on a view:
foo = request.POST.get('foo', None)
Also, I recomend you to use url tag for action
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With