i use this code in python to post something to a form in a web page.
but i dont just want to send some text as an input textbox, i also want to decide if a checkbox in the form is checked or not.
what value do i have to give to the 'checkbox' parameter in the "web_form"?
web_form = [('textbox', text),('checkbox', ?????)]
form_data = urllib.urlencode(web_form)
o = urllib2.build_opener(url)
res = o.open(url, form_data).read()
this is the html of the form:
<form action="?" method="POST" enctype="multipart/form-data">
<textarea name="textbox"></textarea> Checkbox <input type='checkbox' name='cb' > <input type="submit" value="submit" /></div>
</form>
You question is pretty broad since you did not mention with web framework you are using.
Application code could check either for the existence of the checkbox key in the request or check for a particular value...unless you provide further informations: try setting it to something like '1' and see what's happening.
Related
I'm working with django and I want to calculate something depending on what the user put on the selectboxes of my page, but I don't know how to connect that with my python code that calculates everything, please help :(
You can use a html form element like this with method post and action="path_of_your_python_file"
<form name="search" action="calculate.py" method="GET">
Search: <input type="checkbox" name="user_input">
<input type="submit" value="Submit">
</form>
Then in calculate.py
import cgi
form = cgi.FieldStorage()
searchterm = form.getvalue('user_input')
// Do your calculation here
I think that will help
In html, I have a form consisting of three radio button
<form action="{{ url_for('handle_data') }}" method="post">
<input type="radio" name="option" value="banana"> Banana<br>
<input type="radio" name="option" value="apple">apple<br>
<input type="radio" name="option" value="peach"> peach<br>
<input type="submit" value="Submit Text">
</form>
In my app.py, I have the method defined for this:
#app.route('/result/<fruit>', methods=['POST'])
def handle_data(fruit):
fruit = request.form['option']
return render_template("result.html",fruit = fruit)
What I wanna do was redirecting from my current html file to a new URL, and the new URL is corresponding to which radio button I click. For example, if I click "apple" and hit the "submit text" button, I will be redirected to "/result/apple".
I did that in the way above, it gives me a server error. What did I do wrong in this case?
Either do dynamic URLs OR parse form data, you don't need to do both. In this case, I would only use the result URL without the dynamic URL with because your template is expecting your dynamic URL in url_for to be completed. You can still use dynamic URLs, but I believe you would need to add some JavaScript to populate the URL as you click the submit button. Since the template is being rendered before you select a fruit, it doesn't know to pass a fruit at the end of your URL, thus your request would only go to /result/ instead of /result/apple if you submit apple.
#app.route('/result',methods=['POST'])
def handle_data():
fruit = request.form.get["option"]
return render_template("result.html",fruit=fruit)
I am new to python flask and I am trying to validate the form using the same. Below is the HTML code
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload id="my_id">
</form>
When the user clicks on upload, the request is sent to the python flask code. But what I wanted to do is, I wanted to retrieve the ID (my_id in this case) without explicitly specifying it.
if request.method == 'POST':
try:
file = request.files.get('file')
For now I am directly specifying "file" to get the file uploaded. But I wanted to get the ID of the upload button without specifying it directly. Please help me in doing this.
Why I need this is because, I will be adding more upload options in near future. Hence if I get the ID properly, I can directly take in the file uploaded pertaining to that button.
Thank you in advance
There is no way to detect the id of a clicked button's tag from the server, because the id is not sent to the server. What is sent to the server is a list of name:value pairs.
So, one can create three submit buttons with different names and/or values.
<input type="submit" name="submit_button" value="Upload Lion">
<input type="submit" name="submit_button" value="Upload Tiger">
Then, on the server side, the clicked button can be detected:
if request.form['submit_button'] == "Upload Lion":
process_lion()
elif request.form['submit_button'] == "Upload Tiger":
process_tiger()
On the other hand, the <button> tag gives you more freedom, because you can specify a value to be sent to the server to be different from the label, for example:
<button type="submit" name="submit_button" value="lion">Upload</button>
<button type="submit" name="submit_button" value="tiger">Upload</button>
And then:
if request.form['submit_button'] == "lion":
process_lion()
elif request.form['submit_button'] == "tiger":
process_tiger()
Trying to build my own webmail client.
I get a template for the web part which look like this
<form name="input" action="test2.py" methog="get" class="form">
<input type="text" name="Username" placeholder="Username">
<input type="password" name="Pass" placeholder="Password">
<button type="submit" id="login-button">Login</button>
</form>
Would like to get the input on click on the button of Username && Pass via GET method to fill this bunch of code in python.
user = ???(Username)
pass = ???(Pass)
And print to a new empty html file the value of thoses var.
New to python so everything I try seem to fail.
You should use method="post", it won't work with a GET method (however it is misspelled, you wrote "methog").
Write import request without quotes at the top of your python file.
Now use request.inputForm['name'] without quotes to access form elements where name is the element name.
i am doing a small project in django by using python.
In that project, there is a table with list of pending task having columns: task_id,summary,description,due_date.
I have to give 'edit' button in each row so that one can edit the data of that particular row. But I am not able to identify the button click event in view page for the particular row.
Please somebody help me...
I'm assuming you're not talking about browser click events since you're tagging this with python and django only.
You can identify which <input type="submit"> element was pressed by giving it a name.
A user clicking on <input type="submit" value="edit" name="summary" /> will be identified by the presence of a summary key in request.POST
# form snippet
<input type="submit" value="Edit Summary" name="summary" />
<input type="submit" value="Edit Due Date" name="due_date" />
# view snippet
if request.POST.get('summary'):
print('user clicked summary')
elif request.POST.get('due_date'):
print('user clicked due date')
Below is my answer (an example) if you want to use "GET" instead of "POST".
Assuming that you want to get a value from the user input in html textbox whenever the user clicks 'edit' button, and then call a python function (mypythonfunction) that you wrote inside mypythoncode.py. Note that "btn" class is defined in a css file.
inside yourtemplate.html:
<form action="#" method="get">
<input type="text" value="8" name="mytextbox" size="1"/>
<input type="submit" class="btn" value="edit" name="mybtn">
</form>
inside view.py:
import mypythoncode
def mymethod(request):
if(request.GET.get('mybtn')):
mypythoncode.mypythonfunction( int(request.GET.get('mytextbox')) )
return render_to_response('App/yourtemplate.html')