This question already has answers here:
Create dynamic URLs in Flask with url_for()
(6 answers)
Closed 2 months ago.
I am trying to work with querys in Flask. I have a page choose.html where you can click buttons. these should be sent in a query then to page 2. So page 2 should be rendered by Flask with the query e.g.
127.0.0.1/login?username=test&color=3
I have tried the following so far (hard coded):
<button class="continue" id="submit" name="submit">Continue</button>
#app.route('/login', methods=['post', 'get'])
def login():
message = request.args.get('username')
That was the response:
werkzeug.routing.BuildError: Could not build url for endpoint 'login?username=test'. Did you mean 'login' instead?
Found out the answer:
{{ url_for('login', user='foo', name='test') }}
This will create a query like this:
http://127.0.0.1:10000/login?user=foo&name=test
Related
This question already has answers here:
Sending data from HTML form to a Python script in Flask
(2 answers)
Closed 4 months ago.
Hello i am doing a proyect in pyhton with flask an i pretend to introduce a txt and read it.
This code is part of home.html:
<input type="file" id="gameTXT" name="gameTXT" accept="txt">
<input type="submit" id="submitTXT" value="Submit">
and this one is the part of python:
#app.route("/")
def home():
return render_template('home.html')
How can i get the file? I read that i need to put methods=['GET'] but i don't know where to put it
I try to put methods=['GET'] in the app.route("/") but it doesn't work and it's understandable. I expect to get the file
if you want to get the file, you need to first know what is your form method, as you mentioned GET, I assume GET. (But I insert POST just in case).
You need to create the function, mentioning that the method is GET, and then you need to get the data that was send, which is request.form['name']
Get the data received in a Flask request
Referring above, your code should be
#app.route("/", methods=['GET', 'POST'])
def home():
data = request.form['gameTXT']
### do whatever you want with data
return render_template('home.html')
Try adding method in the route() like this :
#app.route("/", methods=["GET"])
def home():
return render_template('home.html')
This question already has answers here:
Sending data from HTML form to a Python script in Flask
(2 answers)
Closed 1 year ago.
I am trying to learn flask.
My login.html file-
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<table>
<tr><td>Name</td>
<td><input type ="text" name ="uname"></td></tr>
<tr><td>Password</td>
<td><input type ="password" name ="pass"></td></tr>
<tr><td><input type = "submit"></td></tr>
</table>
</form>
</body>
</html>
And my main.py file has this-
#app.route('/login',methods = ['POST'])
def login():
uname=request.form['uname']
passwrd=request.form['pass']
if uname=="ayush" and passwrd=="google":
return "Welcome %s" %uname
I am not able to understand how is this able to access login.html without specifying. Also also please explain what is the code in main.py means.
You have to specify the 'html' in flask to access it, however, if you open the html file in browser this will still work since its action is aimed directly at your flask server.
the code of your main.py says that if the in the form sent the data 'uname' and 'pass' are respectively 'ayush' and 'google', the code sends back to the browser a text indicating: "Welcome ayush"
If you want to directly implement the html in your flask web server, you have to create the function and put your html code in templates folder.
from flask import render_template
...
#app.route('/', methods=['GET'])
def code():
return render_template('index.html', name='')
So you can access with http://localhost:5000/ now
This question already has answers here:
Post values from an HTML form and access them in a Flask view
(2 answers)
Closed 3 years ago.
much like this question, I'm trying to follow the simple Flask tutorial for file upload to a flask server. In my specific case, I'm trying to upload an XML file.
The (simplified) HTML I'm using is:
<form action="" method="post" enctype="multipart/form-data">
<input type="file">
<input type="submit" value="Let's go!">
</form>
The request is correctly handled by a if request.method == 'POST': block, so I put in some print statements to troubleshoot:
print('request.method', request.method)
print('request.args', request.args)
print('request.form', request.form)
print('request.files', request.files)
and the result was the following:
request.method POST
request.args ImmutableMultiDict([])
request.form ImmutableMultiDict([])
request.files ImmutableMultiDict([])
What am I doing wrong? I can provide more complete source code if needed.
As always, I found the answer mere minutes after posting this question. I'm answering here to hopefully help someone else.
The problem was that my file input had no name attribute. Thanks to Ben here I was able to fix this problem by adding a name attribute to the file input, and now the file upload is being processed correctly.
This question already has an answer here:
Make a POST request while redirecting in flask
(1 answer)
Closed 5 years ago.
I am working on a Flask project. After successful execution of login function, it should redirect on home page with some data. The data should be send by post. How can this be done??
For send data after successful login you can use url_for within of redirect something as this:
#app.route('/login', methods = ['POST'])
def login():
if request.method == 'POST' and request.form.get("username") == 'admin':
return redirect(url_for('success',data=request.form.get("data")),code=307)
else:
return redirect(url_for('index'))
After success login you can use your data to send with data=data, so in your other view you get that data.
#app.route("/test/argument", methods=['POST'])
def success():
messages = request.form.get('data') # counterpart for url_for()
return messages
This question already has an answer here:
Reference template variable within Jinja expression
(1 answer)
Closed 7 years ago.
I need to pass dynamic values to my python view. My HTML looks like:
<a href="{{ url_for('refresh_page',
lang_code=g.current_lang,
feature='a+z+',
cata={{ att.get('url_slug')}})}}"
>
I need to pass this {{ att.get('url_slug')}} to my Flask View:
#app.route('/<lang_code>/category/<string:feature>/<string:cat>/<int:pag e>')
def navigate_page(feature, page):
But its not working. I just started working on views, what I am doing wrong. Please help!!
The url_for is already a template function, so no need to escape the parameter:
<a href="{{ url_for('refresh_page',
lang_code=g.current_lang,
feature='a+z+',
cata=att.get('url_slug')) }}">