I am trying to extract text from the following HTML code:
#app.route("/", methods=['GET', 'POST'])
def home():
if request.method == 'POST':
H_desiderata = float(request.form.get('H_desiderata')) #THIS CAUSES THE ERROR
return render_template('form1.html')
HTML below:
<body>
<h2>Brioche Recipe</h2>
<form>
<div class="grid-container">
<div class="grid-item">
<label class="text" for="H_desiderata">
H_desiderata:
</label><be>
<input type="number" id="H_desiderata" name="H_desiderata1"
value={{val_H_desiderata}} min="1" max="99" step="1"><br>
Before putting it into a grid it worked:
old working code:
<form>
<label for="H_desiderata">H_desiderata:</label><br>
<input type="number" id="H_desiderata" name="H_desiderata"
value={{val_H_desiderata}} min="1" max="99" step="1"><br>
How should I adapt request.form to return the value within the input box?
There is so much wrong with your code, but let's start with this:
request.form is empty when request.method == "GET. So. request.form['H_desiderata'] will give a key error.
Move that to the POST section of your view. Also, use request.form.get('H_desiderata', -9999999) in case it's not defined.
UPDATE:
OK, now try:
if request.method == 'POST':
print(request.form)
print(request.form.get('H_desiderata'))
print(float(request.form.get('H_desiderata')))
H_desiderata = float(request.form.get('H_desiderata'))
Then, you are going to want:
return render_template('form1.html', val_H_desiderata=H_desiderata)
UPDATE2:
Your <form> tag is malformed. Try:
<form action="/" method="post">
UPDATE3:
You change the name of the input, so change to: request.form.get('H_desiderata1')
Related
This question already has answers here:
Sending data from HTML form to a Python script in Flask
(2 answers)
Closed 1 year ago.
I have been working with flask for a long while, but after a break from it, I cant seem to figure out what's wrong here.
index.html:
<input name="linkHolder" type="url" class="defaultTextBox advancedSearchTextBox link" placeholder="http://www.youtube.com">
<form method="POST" action="/button">
<input class="btn" type="submit">Go</input>
</form>
main.py:
#app.route('/button', methods=["GET", "POST"])
def button():
if request.method == "POST":
dlink = request.form.get("linkHolder")
print(dlink)
return render_template("index.html", dlink=dlink)
I'm sorry if its a simple answer but my end goal here is to load the link typed by the user, print said link, and then reload the page. What am I doing wrong?
In your index.html, your <form> tag does not include the linkHolder input.
Do the following:
<form method="POST" action="/button">
<input name="linkHolder" type="url" class="defaultTextBox advancedSearchTextBox link" placeholder="http://www.youtube.com">
<input class="btn" type="submit">Go</input>
</form>
You might also need an if statement in main.py that actually renders the page
#app.route('/button', methods=["GET", "POST"])
def button():
if request.method == "GET":
return render_template("index.html")
if request.method == "POST":
dlink = request.form.get("linkHolder")
print(dlink)
return render_template("index.html", dlink=dlink)
You need a form with the name
Also, your tag </input> input doesn't have any closing tag you should be using button tag
<form method="POST" action="/button">
<input type="text" name="linkHolder">
<button class="btn" type="submit">Go</button>
</form>
I've looked around but can't seem to find an answer. I've an html form with radio button options in it. I'm trying to append them to the flask url when the results are posted based on the user input on the html page.
Here is my html page:
<form class="form-search" id="formdata" action="{{url_for('users')}}" method="post">
<label><font size="4">Select option:</font></label>
<div class="labeltext btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="radioOptions" id="option1" value="Option1" checked> Option1 </label>
<label class="btn btn-primary">
<input type="radio" name="radioOptions" id="Option2" value="Option2"> Option2 </label>
</div>
</form>
My Flask View:
#app.route('/users/<selectOption>', methods=['GET', 'POST'])
def users(selectOption):
if request.method == 'POST':
radioOptions = request.form['radioOptions']
return redirect (url_for('users', selectOption=selectOption))
return render_template('users.html')
I'm ending up with error
TypeError: users() takes exactly 1 argument (0 given)
Unsure what's going wrong from my end. I'm trying to make my url something look like below:
localhost:8080/users?radioOptions=Option1
TypeError: users() takes exactly 1 argument (0 given)
The above error you get explains it all. In the following code :
if request.method == 'POST':
radioOptions = request.form['radioOptions']
return redirect (url_for('users', selectOption=selectOption))
You redirect to users while submitting the radio option.The users expects an argument which you have not provided yet ,i.e it is changeable and that is why you are getting this error.Plus you are also not using your radioOptions value which you retrieve from this line
selectOption = request.form['radioOptions'] #I have changed radioOptions to selectOption
The correct and cleaner way would be to define another function which renders your template and call it then in your redirect call , something like this :
#app.route('/users', methods=['GET', 'POST'])
def users():
if request.method == 'POST':
selectOption = request.form['radioOptions']
return redirect (url_for('call_selected', selectOption=selectOption))
return render_template('users.html')
#app.route('/<selectOption>', methods=['GET', 'POST'])
def call_selected(selectOption):
return render_template(selectOption)
I'm building a simple web site using flask, witch contain one page html index.html, and sections : search, about, contact. This sections are not displayed until the user enter to them
I want to send a POST request from index.html/#search to the same section : index.html/#search.
I tried to send it like that :
#app.route('/search', methods=['POST'])
def Search():
_text = request.form['text']
return render_template('index.html/#search', result=_text)
but that didn't work, and I get this error :
jinja2.exceptions.TemplateNotFound: index.html/#search
I tried to pass search as parameter :
#app.route('/search', methods=['POST'])
def Search():
_text = request.form['text']
return render_template('index.html', id="search", result=_text)
but when I run it didn't works because it takes me to http://127.0.0.1:5000/search (witch displayed the index.html page) while I want to go to http://127.0.0.1:5000/#search.
This is my index.html page:
<article id="search">
<h2 class="major">search</h2>
<form method="post" action="Search">
<label for="text">Text</label>
<input type="text" name="text" id="text" placeholder="write any thing in your mind :p" required/>
<input name="search" type="submit" value="See Polarity" class="special" />
<input type="reset" value="Reset" />
</form>
<!-- result search -->
{% if result : %}
<p>{{ result }}</p>
{% endif %}
</article>
The # represents an anchor on an existing page, not a new page.
Similarly, your syntax of :
#app.route('/search', methods=['POST'])
Would create a new route to http://127.0.0.1:5000/search . If you want it to render index.html and then jump to the search anchor you should have it render index.html as you've done it, include the _anchor attribute. It would look like:
#app.route('/', methods=['GET','POST'])
def Search():
if request.method == 'GET'
return render_template('index.html')
else:
_text = request.form['text']
return render_template('index.html', _anchor="search", result=_text)
I'm newbie to flask,I have such a form:
<form class="form" action={{ url_for('/login') }} method="POST">
<input type="text" name="usename" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button type="submit" id="login-button">Login</button>
</form>
I use request.form()and request.form.get() to get submitted values,but the result is always the same**"GET / HTTP/1.1" 200 -**
#app.route('/login',methods=['POST','GET'])
def login():
name=request.form['usename']
password=request.form['password']
form_data=request.form.get('usename')
print name,password,form_data
return render_template('login.html')
And I try visiting /login ,it shows Bad Request.But when it is modified to such,it's ok.It seems my request statement block is incorrect.
#app.route('/login',methods=['POST','GET'])
def login():
return render_template('login.html')
It's because of form action change the
action="{{url_for('index')}}"
Your form does not post actually and you are try to get values. And don't forget to add a view function named index or you can use instead login instead index
i guess you are trying to get those values from login.html file. If it is so you have to use something like this
#app.route('/login',methods = ['GET','POST'])
def login():
if request.method =='POST':
name=request.form['usename']
password=request.form['password']
form_data=request.form.get('usename')
print name,password,form_data
return render_template('login.html')
try leaving the action field emtpy.
<form class="form" action="" method="POST">
leaving it empty will post to the same location where the form was downloaded from, which I guess is your use case here.
[1] Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")
I want to ask how to make my urls to include the worh that I searched. Something like this:
http://host/filter?test
My code:
#app.route('/filter', methods=['POST', 'GET'])
def filter():
if request.method == 'POST':
if str(request.form['search_string']) <> '':
api.queryMessage(request.form['search_string'])
return render_template('main.html', search_string=search_string)
And my template, main.html:
<form name="filters" action="{{ url_for('filter') }}" method=post id="filters">
<div style="position: absolute; top:100px; left:300px">
<p>Search string: <input type=text size=80 title="Match all of the words" name=search_string value="{{search_string}}"></p>
<input type=submit value=Filter/Search onclick=loopSelected();>
<input type="hidden" name="chosen" id="chosen" value="{{chosen}}" />
</div>
</form>
You're now using POST request. Use GET request, so browser will put your form values to URL.
in HTML form set method="GET":
<form name="filters" action="{{ url_for('filter') }}" method="GET" id="filters">
You'll get URL in the following form:
http://host/filter?search_string=test&chosen=<id>&<some other trash>
In Flask use request.args instead of request.form:
if request.method == 'GET':
if request.args.get('search_string'):
...