I have an input text field in my form which has an important new attribute added via JavaScript. The attribute name is fieldid.
Is there a way to retrieve this attribute and its value from the view after the form is summited?
Attributes on form elements are not passed on to the server when forms are submitted; only input element values are.
The way to add extra information to be sent back to a server on a form submit is by adding additional <input /> elements to your form, specifically <input type="hidden" /> elements.
E.g.
<input type="hidden" name="foobar" value="spam and eggs" />
adds a new field foobar to your form data with the value spam and eggs.
Related
I have HTML form like this:
<p>Models Sizes IDs:</p>
<input type="checkbox" name="model_size_ids[]" value="1">XS</input>
<input type="checkbox" name="model_size_ids[]" value="2">S</input>
<input type="checkbox" name="model_size_ids[]" value="3">M</input>
<input type="checkbox" name="model_size_ids[]" value="4">L</input>
<button>Submit</button>
I'm trying to receive an array of checked values on server side in my View:
size_ids = request.data['model_size_ids[]']
But, I can extract only one and the last value. So if I check 2-3 values in
checkbox form, I receive only last value in my view. I also tried to name input field without braŃkets and the result was the same. Can anybody tell me, how can I solve that?
Thanks!
Use the getlist method for geting the list of selected choices
request.POST.getlist('model_size_ids[]')
I'm stuck with a task I have currently with django. There are plenty of tutorials on creating dynamic forms but my problem is more about getting a proper HTML.
I have several model instances, let is be
class SomeModel(models.Model):
field1=models.CharField(max_length=20)
field2=models.CharField(max_length=20)
What I need is to show user an HTML page whith multiple forms representing several SomeModel instances.
The problem is I wish that only some of the forms will give user an opportunity to enter only field1, some will only allow to enter field2 and some give both.
For the fields I don't wish to be editable I want to present it's content as a label. So, for instance in my database I have tree SomeModel instances that have field1 and field2 ('aaa','bbb'), ('cccc','dddd') and ('eeeee','fffff'). And I have information that I want only field1 editable in first instance, on the second only field 2 is editable, and it is allowed to edit both field1 and field2.
The HTML that renders it I want looks like somewhat like this
<form method="post" action="">
<input name="some_model_id" type="hidden" value="0">
<label>some Model 0 field1</label><input type="text" name="someModel-0-field1" value="aaa" id="id_form-0-title" />
<label>bbb</label>
<input name="some_model_id" type="hidden" value="1">
<label>cccc</label>
<label>some Model 1 field2</label><input type="text" name="someModel-1-field2" value="dddd" id="id_form-0-title" />
<input name="some_model_id" type="hidden" value="2">
<label>some Model 2 field1</label><input type="text" name="someModel-2-field1" value="eeeee" id="id_form-0-title" />
<label>some Model 2 field2</label><input type="text" name="someModel-2-field2" value="fffff" id="id_form-0-title" />
<input name="Submit" type=submit value="Submit">
</form>
I want to have the reciving the data from the forms as django usualy does, validation, etc.
I know for several forms there are formsets, but I don't know how to use them here.
Also I wish I could first create a form and then say what I want to edit instead of passing this information to __init__.
What is the pythonic way to implement this?
Thank you!
I'm writing a python webapp (on Google AppEngine, using webob) that should process a form containing a list of addresses.
To simplify, lets say the form contains these inputs:
<input type="hidden" name="address[]" value="1" />
<input type="hidden" name="address[]" value="2" />
<input type="hidden" name="address[]" value="3" />
<input type="hidden" name="address[]" value="4" />
Now, Rails\PHP would parse this to a single 'address' value containing a list [1,2,3,4].
Is there a simple way to do this in Python?
from inside a RequestHandler method, self.request.params.getall('address[]') will return a list of the values. The [] is of no significance though, the fields could just as easily be named 'address'.
WebOb uses something called a MultiDict. self.request.POST will take an appropriately form encoded request and return a MultiDict.
From this MultiDict you can call .getall() with the name of a key, in this case your key would be "address[]".
To get a list of entries you would call:
self.request.POST.getall('address[]')
It is not required that you use address[] as the name as MultiDict doesn't use that to identify whether a key can exist multiple times or not.
I use google app engine and python. I want to retrieve the id of an input and not the value. If I use self.request.get("thename") I get the value of the input.
Here is the html
<form method="post">
<input value=" " type="submit" name="thename" id="thenumberIneed"/>
</form>
I cannot put my data on the value tag because I have an image as a background on the input and if I enter there anything, the data shows infront of the image. So, I keep value empty.
If you don't want the value to show, change it to:
<input type="hidden" ... />
But I suspect there are other problems you should be solving, like why does your image get in the way of showing values in the page, and what kind of data you want to retrieve.
You shouldn't be using the id for this type of logic.
You would have better luck using a seperate hidden input field, for example.
<input type='hidden' name='myval' value='thenumberyouneed' />
And then you can just do your search for myval and get your answer.
I need to get values of these check boxes with same name through HTTP "POST".
<input type="checkbox" id="dde" name="dept[]" value="dde"/>
<input type="checkbox" id="dre" name="dept[]" value="dre"/>
<input type="checkbox" id="iid" name="dept[]" value="iid"/>
How to get these values in python using self.request.get() method?
You can use request.get_all().
According to the docs it "Returns a list of values of all of the query (URL) or POST arguments with the given name, possibly an empty list."