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)
Related
I have an html web page. I analyze a sample data and send related plot and table contents to my web page and show them. In the same page I have a text area to get parameter of my next task. after getting the parameter from user, I should do rest of analyze and show another plot and table in the same page. As a result I should have a page like this:
I have app.py (this is part of my code):
#app.route('/', methods=["GET", "POST"])
def index():
tsk1_image = get_from_function_1(data)
tsk2_table_content = get_from_function_2(data)
return render_template('index.html', tsk1_img=tsk1_image, tbl=tsk2_table_content)
and I have index.html (specifically the part of the form is here):
<form action="{{ url_for('index') }}" class="form" method="post" >
<label for="value_k">K:</label>
<input type="text" id="value_k" name="value_k" placeholder="Enter k..." onClick="writeOut()">
<button type="submit" class="btn btn-primary btn-block btn-large">submit!</button>
</form>
Now I want to reload my page with all of data and parameter again and have the final result. I've wrote this part of code but it doesn't work.
if request.method == "POST":
k = request.form.get("value_k")
return redirect(url_for('index'))
Actually I should render_template('index.html', tsk1_img=tsk1_image, tbl=tsk2_table_content, tsk3_img=tsk3_image, tbl2=tsk4_table_content) after I get the value, but I don't know what should I do...
If the page is reloading but not with the data you need, that is because index() is just recalculating the data the same way each time. index() should have a check for if the request is a post, and then if so, use that data in the functions you have. Here's the idea:
#app.route('/', methods=["GET", "POST"])
def index():
my_var = default_value_here
if request.method == 'POST':
my_var = request.form.get("value_k")
tsk1_image = get_from_function_1(data, my_var)
tsk2_table_content = get_from_function_2(data, my_var)
return render_template('index.html', tsk1_img=tsk1_image, tbl=tsk2_table_content)
This way your form data will be used when you reload the data.
I'm trying to create a dynamic URL based on user's input from an HTML form.
For example, if a user types in 'AAPL' or 'KO', the next page should be:
webapp.com/result/AAPL or webapp.com/result/KO
index.html:
<div class="cell" id="cell-1-2">
<span class="data" style="text-align: center;">
<form action="{{ url_for('ticker_result', variable='variable') }}" method="POST">
<input type="text" name="variable" placeholder="search ticker or company" maxlength="4"
font-size="24px" style="text-transform:uppercase">
<input class="button" type="submit" value="Search" onclick="tickerSymbol();">
</form>
</span>
</div>
I've tried renaming the 'variable' part to several different things and nothing works so I'm just stuck at this point.
main.py:
# Routing to the homepage
#app.route("/")
def markert_hours_today():
return render_template(
"index.html")
# Routing to the result page
#app.route("/result/<variable>", methods = ["GET", "POST"])
def ticker_result(variable):
if request.method == "POST":
result = request.form["variable"]
return render_template(
"result.html",
result=result)
When I run the local environment, and type in a ticker symbol, the next URL is:
webapp.com/result/variable
I'm assuming it's HTML that I need to edit? I've read the quickstart documentation for Flask which isn't making sense to me and looked up similar questions but I can't seem to find an answer to this.
You are coming at it from the wrong way. When Jinja creates index.html based on your template you can't know what variable is going to be, so how is Jinja supposed to know?
What you need to do is send the form back to the same route and then redirect the user to whatever they typed in. So something like this:
from flask import request, redirect, url_for
#app.route("/", methods=['GET', 'POST'])
def markert_hours_today():
if request.method == 'POST':
result = request.form['variable']
return redirect(url_for('ticker_result', variable=result)
return render_template("index.html")
Edit: forgot to rename variable from user_input to result.
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 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')
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="")