Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Additional POST data for django

Tags:

post

forms

django

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?

like image 603
Alison Avatar asked Dec 26 '22 16:12

Alison


2 Answers

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')
like image 190
miki725 Avatar answered Jan 04 '23 14:01

miki725


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.

like image 23
Serhii Holinei Avatar answered Jan 04 '23 14:01

Serhii Holinei