Home > Blockchain >  How to print data using flask
How to print data using flask

Time:10-03

I will start by showing my code

<h1>
<form>
    <div >
      <label for="product">Name the product you want to add</label><!-- if value isn't a string invalid -->
      <input type="text" id="product" name="prodcut">
    </div><br><br>
    <div >
      <label for="price">Price for your product</label> <!--if value isn't number invalid -->
      <input type="text" id="price" name="price">
    </div>
    <button type="submit" >Add</button>
  </form>
I want to create database of products so I need to get information about the input. there might be better way to do so but I am a complete beginner so this is what I got. I am using bootstrap btw.

to print out the data I have this code:

@auth.route('/add',methods=['GET','POST'])
def add_products():
data = request.form
print(data)
return render_template("add.html", content = "add")

the output I get is : ImmutableMultiDict([]). even though I give id and name so I don't really get why there is no output after I write and click the add button. after inserting input in the urlbar I can see what I wrote, I also want to get rid of that. Thanks for any help!

CodePudding user response:

You need to specify the method for the <form> tag:

<form method="post">

Also in your route you need an condition to check which type of method is being submitted to the route:

@auth.route('/add',methods=['GET','POST'])
def add_products():
    if request.method == "POST":
        data = request.form
        print(data)
    return render_template("add.html", content = "add")

Since you are a beginner I would highly recommend follow this tutorial by Corey Schafer, It is beginner friendly and by the end you will learn concepts that you will use in the real world.

  • Related