This question already has answers here:
Get the data received in a Flask request
(23 answers)
Closed 6 years ago.
I'm uploading a file to my flask backend and I can't figure out how to access the parameter values in the multipart form.
I can access the uploaded file easily by doing file = request.files['file'] but can't figure out a way to get the parameter values.
I've tried the following but haven't had any luck:
print(request.data['share'])
print(request.data['title'])
print(request.get('share'))
print(request.get('title'))
Most form inputs can be retrieved as follows:
request.form.get("fieldname")
Files can be accessed via
request.files.get("fieldname")
Where the fieldnames are the name attribute in the HTML.
Keep in mind that, just because there's a result for request.files.get("someName") doesn't mean a file was actually uploaded. You should check that the filename exists, too, in order to validate if a file was indeed uploaded.
Take for example, the following HTML
<form action="/form_endpoint" method="POST">
<input type="text" name="data">
<input type="submit" value="submit">
</form>
You would access the value the user input in the data field by data = request.form.get("data")
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 am relatively new to Flask/Python/HTML so please excuse my language.
Basically I am trying to retrieve the "name" field with request.form.get that was inputted in my HTML page. The name field was generated with a for loop in jinja. When i hit the checkbox, and click submit, i expect the request.form.get to retrieve the "name" field that was in that specific check boxes' data. However, when i test out the data, I get a 404 error that says NONE for the request.form.get value. I'm not sure where I am going wrong. I suspect it might be what I am plugging in as the name for request.form.get.
On my flask side:
#app.route("/recipedata", methods=["GET","POST"])
def recipedata():
if request.method == 'POST':
food = request.form.get("{{value.id}}")
On HTML side:
{% for value in foodinfo.results %}
<form action="/recipedata" method = "POST">
<input type="checkbox" name="{{value.id}}" value={{value.id}}>
<input type=text placeholder="{{value.id}}">
<input type="submit"> Go to Recipe info
</form>
{% endfor %}
The 2nd line in my form tag with type text was used to test whether my value.id was printing correctly in Jinja, and indeed it was. Additionally, for clarification, foodinfo is passed as a .json() object with nested dictionary key/values. Value.id allows me to access that dict's value at key 'id', I believe.
Thank you!
I don't think your function definition of recipedata() is valid as per python syntax. You need to indent code in python to preserve scope information. You can see more here.
Try with following function definition.
def recipedata():
if request.method == 'POST':
food = request.form.get("{{value.id}}")
I'm not sure if HTML part is causing any trouble.
I tried looking at different resources on the internet regarding this request and request.data in django, but I couldn't fully understand it.
Why this request parameter is kept inside the function? What are we passing in this request parameter?? Also, what does this request. data do??
def index(request):
content = {
'Blogdata': Blog.objects.all(),
}
return render(request, 'index.html', content)
def somefunction (request):
data=request.data
As you can see I have two functions above both of them have request paramter inside the function. Also, I need the explanation on this request.data as this has to be used multiple times.
First, you should understand about HTTP Request(Header, Body). When you type in form and send to server, browser get data with name and add values into body request. In the backend server, we will get data from body with name.
Example:
I have form fill your name:
<form action="/signin" method="get" name="myForm">
<label for="name">Your name:</label>
<input type="text" id="name" name="name"><br><br>
<input type="button" value="Send form data!">
</form>
You type name : "Khoa", browser get values "Khoa" from input and add key:values with name in . Like this: "name": "Khoa"
In server django, you can get data with using request.data.get("name") = "Khoa"
request.data is body HTTP send to servere, "name" is key part of body have values is "Khoa"
This question already has answers here:
Get the data received in a Flask request
(23 answers)
Closed 2 years ago.
I am trying to create a web application in Flask, but I keep getting the error:
'Request' object has no attribute 'Name' when sending data from a 'POST' form in my html. I have two submit buttons that I have to differentiate, and I wanted to do that by using their name, which I have previously done and it worked. This is the code from my HTML:
<input type = "submit" class="fadeIn second" value="Add this data." name = "add">
<input type = "submit" class="fadeIn second" value="Delete this data." name = "delete">
and this is where i am getting the error:
if request.method == 'POST' and request.Name=="add":
Can anyone help me out, please?
As the error states, a Request object does not have a "Name" attribute. You can access the name value with request.form.get("add"), for example.
See here for more examples.
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.
I'm having issues with correctly sending and receiving a variable with a GET request. I cannot find any information online either. From the HTML form below, you can see I'm sending the value of 'question' but I'm also receiving 'topic' from a radio button in the form (though the code is for that is not below).
I want to send 'topic' using POST but use GET for 'question'. I'm aware that the form method is POST though I'm not sure how to cater for both POST and GET.
HTML Form:
<form method="POST" action="{{ url_for('topic', question=1) }}">
My second issue is that I'm unsure how to receive 'topic' AND 'question' from the form. I've managed to receive 'topic' as seen below but I'm not quite sure how to receive 'question'. Preferably it would be better for the URL to be like so:
www.website.com/topic/SomeTopic?question=1
For the code below, I found online that request.args[] is used for receiving GET requests though I'm not sure if it is correct.
Flask:
#app.route('/topic/<topic>', methods=['POST', 'GET'])
def questions(topic):
question = request.args['questions']
return render_template('page.html')
The question is
How do I send two variables from a form using GET and POST for different variables at the same time.
How would I go about receiving both variables?
The short answer to your question is that you can't send both GET and POST using the same form.
But if you want your url to look like you specified:
www.website.com/topic/SomeTopic?question=1
then you're almost there. First you will need to already know the name of the topic as you have to specify that in your call to url_for() for the questions url.
<form method="GET" action="{{ url_for('questions', topic_name="cars") }}">
# Your url will be generated as www.website.com/topic/cars
flask
# Note that I changed the variable name here so you can see how
# its related to what's passed into url_for
#app.route('/topic/<topic_name>')
def questions(topic_name):
question = request.args['question']
return render_template('page.html')
Now when you submit your form, your input will be sent as a GET, an asumming you have an input field with the name question you'll be able to get the value of that field.