In a multi-page survey application I am creating I have a jQuery UI slider bar which is used to provide a rating for an image. This returns a numerical value to my Python/Django view which is stored in a list slider_DV_values
On a later Data Verification survey page the participant is given the opportunity via another jQuery slider bar to update the rating they assigned the image.
My issue is that the jQuery UI slider bar only returnes a numerical value if the participant changes it. Therefore the original rating is getting overwritten, with nothing, if the participant does not update it.
However if they do update their rating the new value is getting stored.
If I try
elif step == 13:
slider_value1 = self.request.POST.get('slider_value1')
print "This is slider_value1", slider_value1
if slider_value1 is not None:
slider_DV_values.pop(0)
slider_DV_values.insert(0, slider_value1)
The original values stored in slider_DV_values are still getting overwritten, with nothing.
I thought the is not None would have prevented an empty value from being used to overwrite the original value? IS this not correct?
Can anyone tell me how to prevent the original values from getting overwritten unless the new value is an updated numerical value?
Thanks, Deepend
EDIT
To see how I am getting my values this is the jQuery slider bar in a page of my SurveyWizardView the value of which is returned via the hidden form element
<div class="DV_image_row">
<div class="DV_image_left">
<img src="{% static "survey/images/pathone/" %}{{first_image}}{{fourth_image}}{{seventh_image}}" height="300" width="250" style="border:1px solid black;" align="middle"/>
<div class="DV_slider_one" id="one"></div>
<script >
$('#submit').click(function() {
var username = $('#hidden').val();
if (username == "") username = 0;
$.post('comment.php', {
hidden: username
}, function(return_data) {
alert(return_data);
});
});
$(".DV_slider_one").slider({
animate: true,
range: "min",
value: {{first_slider}}{{forth_slider}}{{seventh_slider}},
min: -100,
max: +100,
step: 1,
slide: function(event, ui) {
$("#slider-result_left").html((ui.value > 0 ? '+' : '') + ui.value);
if($(this).attr("id") == "one")
$("#hidden2").val((ui.value > 0 ? '+' : '') + ui.value);
}
});
</script>
<div id="slider-result_left">{{first_slider}}{{forth_slider}}{{seventh_slider}}</div>
<input type="hidden" name="slider_value1" id="hidden2"/>
</div>
"" is None is False
ONLY None is None is True
perhaps you just want if not slider_value1 : which is true for ANY falsey value (empty string,empty list, empty tuple, false, 0 , None , etc)
You might want to try the simple if slider_value1 != '' . This is a better option than if not slider_value1 because the latter will block out 0 also which you might not want to do.
Related
I am using WEB2PY framework
For example, I have a lot of submit type buttons on my webpage and when I click one I want to pass that specific clicked button ID from HTML to a Python variable.
Another problem is that when I use a Python variable inside of a SQlite insert statement then I am getting an error.
My HTML code:
<form method = "POST">
Case:
<input type="text" name="case_qty" value = 0><br>
Pcs:
<input type="text" name="pcs_qty" value= 0><br>
<button type="submit">Add to cart</button>
</form>
PYTHON CODE:
def fortune():
case = 0
pcs = 0
proddict = {}
productrows = db(db.products).select()
//my db contains product id & name
for x in productrows :
proddict[x.id]= x.product_name
if request.post_vars:
case = int(request.post_vars.case_qty)
pcs = int(request.post_vars.pcs_qty)
product= proddict[]//i want submitted button id number inside []
sql = "insert into cart(product_name,case_qty,pcs_qty)values(product,case,pcs)"
// another problem is when i use variable
//inside values i am getting error
r = db.executesql(sql)
return locals()
Making form with deform and whould like to change the pageShema class depending on the choices made by the user.
Ex. if he selects option 1 from selectwidget, show him one set of fields, if case of other choice - another.
How to do this?
You can use jquery and select with the "name" attribute.
Also, you can use the jquery "parent()" function to get the container of the input you are interested in showing/hiding.
For instance, in your schema do something like:
# This is the "<select>"
choices = (('yes','Yes'),
('no', 'No'))
bar = colander.SchemaNode(colander.String(),
widget=deform.widget.SelectWidget(values=choices))
# This is the input that should appear only when the "yes" value is selected
foo = colander.SchemaNode(colander.String())
Then, in your template, add somethig like:
<script>
$( document ).ready(function() {
// ensure that the "foo" input starts hidden
var target = $("input[name=foo]").parent().parent();
target.hide();
$("select[name=bar]").on('change', function(){
var valueSelected = this.value;
if (valueSelected == "yes") {
target.show();
} else {
target.hide();
}
});
});
</script>
How do you get the actual value of the input id after you send it in Flask?
form:
<form action="" method="post">
<input id = "number_one" type="text" name="comment">
<input type="submit" value = "comment">
</form>
like, what I am trying to say is when the form is sent (i.e. when you do this):
request.form.get("comment")
the value of the text field is passed. What I can't figure out is how to get the value of the id.
So, when the form is sent we could then tell from which form the info was coming from, because each form has a unique id. In this case the id is number_one.
So, how do we go about getting the actual literal value of the id and not the text input?
You can't. The id value is not part of the form data set sent by the browser.
If you need to identify the field, you'll have to either add the id to the input element name, or if the id is generated by Javascript code, perhaps store the information in an extra hidden field.
Adding the id to the name could be done with a delimiter perhaps:
<input id = "number_one" type="text" name="comment.number_one">
which would require you to loop over all form keys:
for key is request.form:
if key.startswith('comment.'):
id_ = key.partition('.')[-1]
value = request.form[key]
There is a one way to identify the fields and also multiple forms together....
use this
<input value = "1" type="hidden" name="my_id">
so at your view file you can retrieve this value.
my_id = request.form.get("my_id","")
print my_id
your output will be
1
you can also do this...
my_id = request.form.get("my_id","")
if my_id == "1":
** for example **
value = request.form.get("any_parameter_of_form","")
I need to check whether a link's disabled attribute is set, in the following code,
<a id="ctl00_ContentPlaceHolder1_lbtnNext" disabled="disabled">Next</a>
However on the last page if I execute,
next_pg=driver.find_element_by_xpath("//a[#id='ctl00_ContentPlaceHolder1_lbtnNext']")
next_pg.click()
print next_pg.is_enabled()
I get True as the output, which should not be the case.
Also, only on the last page is the Next coded as given above, in all other pages it is coded as follows, due to which on checking the is_enabled() tag, an error is produced.
<a id="ctl00_ContentPlaceHolder1_lbtnNext" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$lbtnNext','')">
How should I solve this?
Use this answer to get the attributes of the tag:
attrs = driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', next_pg)
and check for the presence of the disabled tag and it's value:
if 'disabled' in attrs and attrs['disabled'] == 'disabled':
# ...
I have a form that allows me to add to a multi list using js. I want to be able to post all the data in that list to my bottle server, but I am not able to get any of the data in there. How do I get all the items in my statement to be posted to server.py? How do I access this post data once it is posted?
Relevant code:
server.py:
#bottle.route('/saveList', method='POST')
def save_list():
forms = bottle.request.get('the_list')
print forms # returns 'None'
return bottle.redirect('/updatelist') # just redirects to the same page with a new list
list.tpl
<select multiple="multiple" id="the_list" name="the_list">
%for item in my_ list:
<option>{{item}}</option>
%end
</select>
EDIT:
I am trying to get the whole list, not just the selected values. The user adds to the multi by way of textfield, button, and JS; so I want to get all the values (or all the new values).
EDIT 2:
I used the answers provided along with some js to get the desired result:
$('.add_to_the_list').click(function (e) {
...
var new_item = $('<option>', {
value: new_item_str,
text: new_item_str,
class: "new_item" // the money-maker!
});
...
function selectAllNewItem(selectBoxId) {
selectBox = document.getElementById(selectBoxId);
for (var i = 0; i < selectBox.options.length; i++) {
if (selectBox.options[i].className === "new_item") { // BOOM!
selectBox.options[i].selected = true;
}
}
}
...
$('#submit_list').click(function (e) {
selectAllNewBG("the_list")
});
You were close; just try this instead:
all_selected = bottle.request.forms.getall('the_list')
You'll want to use request.forms and getall. request.forms returns a MultiDict, which is the appropriate data structure for storing multiple selected options. getall is how you retrieve the list of values from a MultiDict:
for choice in all_selected:
# do something with choice
or, more simply:
for selected in bottle.request.forms.getall('the_list'):
# do something with selected
To get multiple values back use .getall. Here is the code I was able use this with.
import bottle
#bottle.route('/saveList', method='POST')
def save_list():
forms = bottle.request.POST.getall('the_list')
print forms
return bottle.redirect('/updatelist')
#bottle.route('/updatelist')
#bottle.view('index')
def index():
return {}
bottle.run()
The HTML
<html>
<body>
<form method="post" action="http://localhost:8080/saveList">
<select multiple="multiple" id="the_list" name="the_list">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
<input type="submit" />
</form>
</body>
</html>
The output to stdout looks like:
127.0.0.1 - - [22/Mar/2014 13:36:58] "GET /updatelist HTTP/1.1" 200 366
['1', '2']
127.0.0.1 - - [22/Mar/2014 13:37:00] "POST /saveList HTTP/1.1" 303 0
127.0.0.1 - - [22/Mar/2014 13:37:00] "GET /updatelist HTTP/1.1" 200 366
[]
First time selected two objects, second time didn't select any.