-1

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!

keinn12
  • 1
  • 1
  • `request.form.get("{{value.id}}")` instead `{{value.id}}` give the valye of `value.id`. Basically whatever you see as `name=??` in html. `request.form` is a `dict` you can print it also to see what are available `print(request.form` – Epsi95 Sep 08 '21 at 03:56
  • How do i actually retrieve the value id from my HTML template? request.form.get("{{value.id}}") am i actually getting the value itself or am i literally seeing {{value.id}}? – keinn12 Sep 08 '21 at 13:26

1 Answers1

1

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.

avats
  • 437
  • 2
  • 10
  • How do i actually retrieve the value id from my HTML template? request.form.get("{{value.id}}") am i actually getting the value itself or am i literally seeing {{value.id}}? – keinn12 Sep 08 '21 at 13:27
  • @keinn12 `request.form` is a python dict and by using `dict.get("key")`, we can fetch the desired value from the dict. Here, **{{value.id}}** is the key. So, when you are doing `food = request.form.get("{{value.id}}")`, the value is stored in the food variable and you can use it now as a normal python var. – avats Sep 11 '21 at 12:09