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.
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
I'm using a Tornado server running with Python, whose job in convert a .svg file in several font formats. For now I managed to make it generate a fontpack in a .zip file by clicking on a button. My Python code links with html form by using self.request.files['filearg'] where filearg is the name of the file selected in an <input type="file"> box.
But I'd like to make this submit button communicate with a text box in which I'd fill the output format I want my file converted in.
Basically, what I'd want in my html form would look like this:
<form enctype="multipart/form-data" action="/upload" method="post">
File: <input type="file" name="filearg" />
<input type="text" value="" name="format"/>
<input type="submit" value="Generate font(s)" />
</form>
I managed to do it with a Node.js server (with fields.format and so on), but I can't find any way to do this in Python.
I've heard about the CGI forms in HTML, which can permit to get this kind of content. But as if it's not the kind of form I chose, I wonder whether it can be implemented with my actual form.
The Tornado documentation explains how to get the values of form fields, using get_argument:
format = self.get_argument("format")
I want to fill out this form using Python:
<form method="post" enctype="multipart/form-data" id="uploadimage">
<input type="file" name="image" id="image" />
<input type="submit" name="button" id="button" value="Upload File" class="inputbuttons" />
<input name="newimage" type="hidden" id="image" value="1" />
<input name="path" type="hidden" id="imagepath" value="/var/www/httpdocs/images/" />
</form>
As you can see, there are two Parameters that are named exactly the same, so when I'm using Mechanize to do it, what would look like this:
import mechanize
br = mechanize.Browser()
br.open('www.site.tld/upload.php')
br.select_form(nr=0)
br.form['image'] = '/home/user/Desktop/image.jpg'
br.submit()
I am getting the Error:
mechanize._form.AmbiguityError: more than one control matching name 'image'
Every solution I found in the Internet (including this site) didn't work. Is there a different approach?
Renaming the input in the HTML form is sadly not an option.
Thanks in Advance.
You should use find_control instead; you can add a nr keyword to select a specific control if there is ambiguity. In your case, the name and type keywords should do.
Also note that a file control doesn't take a value; use add_file instead and pass in an open file object:
br.form.find_control(name='image', type='file').add_file(
open('/home/user/Desktop/image.jpg', 'rb'), 'image/jpg', 'image.jpg')
See the documentation on forms in mechanize.
I'm two days in to Python and GAE, thanks in advance for the help.
I have an input array in HTML like this:
<input type="text" name="p_item[]">
<input type="text" name="p_item[]">
<input type="text" name="p_item[]">
I want to parse the input in Python, and I'm trying this, which isn't working:
items = self.request.get('p_item')
for n in range(1,len(items)):
self.response.out.write('Item '+n+': '+items[n])
What is the correct way to do this?
Change your html to this
<input type="text" name="p_item">
<input type="text" name="p_item">
<input type="text" name="p_item">
and use the self.request.get_all() method http://code.google.com/appengine/docs/python/tools/webapp/requestclass.html#Request_get_all
p.s. For reference, there is no concept of arrays for GET/POST data, your form gets transformed a key=value string separated by '&' e.g.
p_item=1&p_item=3&p_item=15
etc, it's up to the web framework to interpret whether a parameter is an array.
Edit: oops, just read the comments that you figured this out already, oh well :P
I would recommend doing some debugging if this sort of issue comes up. Make things simple and write out your variable values and ensure you get what you expect at each step. Do something like the following:
<form method="get">
<input type="text" name="single_key" />
<input type="text" name="array_key[some_key]" />
<input type="submit" />
</form>
And see what happens when running the following Python on the backend:
single_value = self.request.get('single_key')
self.response.out.write(str(single_value))
array_value = self.request.get('array_key')
self.response.out.write(str(array_value))
Based on the output you should have a better idea of what to get the desired results or how to add more detail to your question if you still don't understand a certain behavior.
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.