Getting " 'Request' object has no attribute 'Name'" in Flask [duplicate] - python

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.

Related

Flask and Retrieving data from HTML [duplicate]

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.

Why BadrequestKeyError is showing?

I am making a small project - Reminider System. I have a form which accepts values from users and inserts into the database table. The problem is occurring while fetching a value from a textbox. Below is my code and also I am giving what error am getting.
<form method="POST" action="">
<input type="hidden" name="unique" value="{{session.UID}}" disabled="true">
<button type="submit" class="btn btn-primary">Confirm</button>
</form>
This is my template
#app.route('/home/set_reminder',methods=['POST'])
#is_logged_in
def set_reminder():
if request.method=='POST' and form.validate():
uid = request.form['unique']
I am getting the error in this line uid = request.form['unique']. Not getting why it cannot fetch the value.
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'unique'
And this is the error which am getting.
Please help me out.
In your html, the uid input is disabled, so the browser will not send uid in a POST request's body. This causes the error when you try to access request.form.uid - it doesn't exist.
You could use readonly rather than disabled if the value must be returned by the browser.
See this answer for a little more information on BadRequestKeyError.

Flask request.files is empty [duplicate]

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.

view multipart form request parameter values using flask [duplicate]

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")

How can I fix this attribute error?

Am working on a web2py HTML view but keep getting an erros.
This is the code:
{{extend 'layout.html'}}
<h2>Edit your post</h2>
<h3>for category {{=form.record.category.name.title()}}</h3>
{{=form}}
and the error:
AttributeError: 'NoneType' object has no attribute 'name'
How can i fix the error?
N/B controller:
def edit_post():
id = post.id
form = SQLFORM(= A("Edit post",_href=URL(request.args=auth.user.id/login))
return locals()
Please see the SQLFORM documentation about how you create the form. I assume you've changed the code before you posted it up here, since python wouldn't compile it because of the = in the parameter list of SQLFORM.

Categories