Python Bottle how to read request parameters - python

I am using http://dingyonglaw.github.com/bootstrap-multiselect-dropdown/#forms to display a dropdown with multiple check boxes.
<li>
<label>
<input type="checkbox" name="filters" value="first value">
<span>First Value</span>
</label>
</li>
<li>
<label>
<input type="checkbox" name="filters" value="second value">
<span>Second Value</span>
</label>
</li>
This is the resulting URL:
http://example.com/search?filters=first+value&filters=second+value
On Server side (bottle):
terms = unicode (request.query.get ('filters', ''), "utf-8")
will give me only "second value" and ignore "first value". Is there a way of collecting all the 'filters' values?

Use the request.query.getall method instead.
FormsDict is a subclass of MultiDict and can store more than one value per key. The standard dictionary access methods will only return a single value, but the MultiDict.getall() method returns a (possibly empty) list of all values for a specific key.

Hey I had the same problem and figured out the solution
I'll write code that applies to your problem
HTML: (noted, I'm not super proficient here so there may be an error but the basic structure is correct). Here we want to setup a "form action" and use method=GET
<form action="/webpage_name" method="GET">
<li>
<label>
<input type="checkbox" name="filters" value="first value">
<span>First Value</span>
</label>
</li>
<li>
<label>
<input type="checkbox" name="filters" value="second value">
<span>Second Value</span>
</label>
</li>
<input type="submit" name="save" value="save">
</form>
Python:
The variable "all_filters" will take all the data from "filters" variable
from bottle import request
#route('/webpage_name', method='GET')
def function_grab_filter():
if request.GET.save:
all_filters = request.GET.getall('filters')
for ff in all_filters:
fft = str(ff[0:]) # you might not need to do this but I had to when trying to get a number
do soemthing....

Related

automate filling in web forms

There is a text box (ID) on the website and I want to put 10,000 data in it. After placing each data in this text box, results are displayed in other text boxes according to the ID.
Textbox ID :
<input class="form-control ltr left text-box single-line" data-val="true" data-val-regex="It is 10 numbers" data-val-regex-pattern="^[0-9]{10}$" data-val-required="*required" id="NId" maxlength="10" name="NId" onblur="LoadInfo()" type="text" value="">
Textobx Name (fill after enter the ID in text box) :
<form action="/Employees/Manager/SavePerson" data-ajax="true" data-ajax-begin="onpostcreatebegin" data-ajax-complete="onpostcreatecomplete" data-ajax-loading="#ajaxloading" data-ajax-method="post" data-ajax-mode="replace" data-ajax-update="#result" id="form0" method="post" novalidate="novalidate"><input name="__RequestVerificationToken" type="hidden" value="asdxBadsP7CpS53654as6dadH3865asdadKhjasdad"> <input type="hidden" name="empId" id="empid" value="0">
<div class="panel panel-info">
<div class="panel-body">
<input data-val="true" data-val-regex="It is 10 numbers" data-val-regex-pattern="^[0-9]{10}$" data-val-required="*required" id="NId" name="NId" type="hidden" value="1234567890">
<input data-val="true" data-val-number="The field PersonId must be a number." data-val-required="The PersonId field is required." id="PersonId" name="PersonId" type="hidden" value="254102232">
<input data-val="true" data-val-required="The GuidId field is required." id="GuidId" name="GuidId" type="hidden" value="665xs6asd-cxc2-wq56-8888-30654998b166">
<div class="form-group">
<div class="col-md-4">
<input class="form-control text-box single-line" data-val="true" data-val-regex="enter the name." data-val-regex-pattern="^[\u0600-\u06ff\s]+$|[\u0750-\u077f\s]+$|[\ufb50-\ufc3f\s]+$|[\ufe70-\ufefc\s]+$|[\u06cc\s]+$|[\u067e\s]+$|[\u06af\s]$|[\u0691\s]+$|^$" data-val-required="*required" id="Firstname" name="Firstname" type="text" value="jack">
<span class="field-validation-valid text-danger" data-valmsg-for="Firstname" data-valmsg-replace="true"></span>
</div>
</div>
</div>
</div>
</form>
The page will not reload after entering the value in the text box ID. Shows the value on that page. What solution do you suggest? I do not know which method to consider that is the most optimal method. Can you introduce the most optimal and practical method?
Depending on how complex the task is, you can use either Selenium (Selenium is a script-controlled Browser) with Firefox WebDriver, for example.
It is of course also possible to use the request methods GET and POST as a method with more work but much higher performance.

FLASK app: Sending data from a Form through another script while one "input" is a dynamic paragraph

I have a from with three possible inputs to submit and send through a seperate script that then generates JSON data.
The problem is however while two inputs are actual inputs = one email and one being a nummer. The third one is not a really traditional input.
<form action="{ url_for('handle_data') }}" method="POST">
<div class="form-group">
<label for="Speryear">SPER jaar</label>
<input class="form-control" type="number" value="2" name="Speryear" min=0 max=10 />
</div>
<div class="form-group">
<div class="form-group">
<label for="inputEmail">Verzendings mail</label>
<input class="form-control" type="email" name="inputEmail" required />
</div>
</div>
<div class="form-group">
<div class="url-panel">
<p> <b>Url:</b></p>
<p id="api-url" name="api-url"></p>
</div>
</div>
<button id="search" type="submit" class="btn-primary">
Aanvraag indienen</button>
</form>
#app.route('/handle_data', methods=['POST'])
def handle_data():
sper_year = request.form["Speryear"]
email = request.form["inputEmail"]
url = request.form["api-url"]
Requested_data = GIPOD_converter.main(url, sper_year, email)
return Requested_data
The third input is actually a paragraph which is dynamically based on the values of a second form (the primary from) for the data requests. According to this post here:
Sending data from a html non-input to Flask
HTML forms only send along tagged values to the remote endpoint when a "submit" input is pressed.
I have tried to make this paragraph a data input but the thing is this will break the javascript I have for that specific id. Aka a the URL part that I want cannot be generated in the input field. So can my code get the paragraph from this?
Edits done as per answer.
I think you should end the app route with:
return Requested_data
Also, you do not define correctly to the url form, i.e.:
url = request.form["api-url"]

How to disable options in form based on current data in database in Django?

I have a form where users can enter a timestamp whenever they have messed up the time they submitted previously. I am changing it so that the timestamp submitted can be either an entry or an exit, based on the button the user clicks. Right now, I have two boxes for the user to enter their timestamp, to know whether is an entry or an exit timestamp, like shown below:
<div class="checkbox">
<label for="chk"><input type="checkbox" value="0" id="chk" /> Requires manager approval</label>
</div>
<div style="padding-top: 10px; text-align: center; display: none" id="appreq">
<input type="checkbox" value="0" id="modinchk" /> Modify Entry
<div style="display: none" id="modin">
<label>Time In</label>
{% standard_input form.modified_in datetimepicker=True hide_label=True %}
</div>
<input type="checkbox" value="0" id="modoutchk" /> Modify Exit
<div style="display: none" id="modout">
<label>Time Out</label>
{% standard_input form.modified_out datetimepicker=True hide_label=True %}
</div>
</div>
Is there a way to change it so that there is some sort of temporary variable, so that I can have only one box where they enter the date/time and when they click either enter or exit, the temporary variable data moves to either modified_in or modified_out? I imagine this second part would be handled in views, but I don;t know how I can have a temporary variable instead of sending it directly to modified_in/out. Should I just create a temporary timestamp to handle in views?

Multiple inputs with same name through POST in bootstrap?

Multiple inputs with same name through POST in php
I've looked at this and I was hoping I could get something similar with python and bootstrap but it just shows Nonetype with "key[]" or the last value if I just use "key".
<div class="form-check">
<label class="form-check-label" tal:repeat="a keys">
<input type="checkbox" name="key[]" value="${a.id}">${a.name}
</label>
</div>
Is there a way to do this?

python selenium checkbox nth element if text

I am using python selenium and need to reach to the element, where the label is "thisis2ndID" and enable the checkbox?
<h3 class="activator"> … </h3>
<div class="accordion-panel">
<ul>
<li class="field">
<input id="thisis1stID" type="checkbox" name=""></input>
<label for="thisis1stID"> … </label>
</li>
<li class="field">
<input id="thisis2ndID" type="checkbox" name=""></input>
<label for="thisis2ndID"> … </label>
</li>
<li class="field">
<input id="thisis3rdID" type="checkbox" name=""></input>
<label for="thisis3rdID"> … </label>
</li
tried these but they dont seem to work...
browser.find_element_by_id('main-id').click()
browser.find_element_by_xpath("//div[#class='accordion-panel']")
browser.find_element_by_id(thisis2ndID').is_selected()
any ideas?
I have done this a couple of different ways, but the best method in your case is to iterate all the li tags and search for the id value you are looking for, try this
browser.find_element_by_xpath("//div[#class='accordion-panel']")
for i in browser.find_elements_by_tag_name("li"):
...
If there is provision to find elements using cssSelector in python, the follwing selector along with click() event will enable the checkbox....
"div.accordion-panel li.field+li.field>#thisis2ndID"
I've worked only on java, so i wont be able to provide exact code in python..
you can click here to know more about different kind of selectors
this worked...sorry for the confusion, the activator needed to be clicked to load the sub options...
browser.find_element_by_class_name('activator').click()
browser.find_element_by_xpath("//div[#class='accordion-panel']")
browser.find_element_by_id('thisis2ndID').click()
time.sleep(5)

Categories