i cant get data with request form code from html to app on flask

1.4k views Asked by At

i have tried so many time to correct flow but i cant find it , help me where i am wrong. when i want to take data from form , it says there is no defined variable.

i have allready created database and i want to compare with this database and user input then show the same page results.

EDIT : I CANT GET 'veri' value with request.form['veri']

class Products(db.Model):
    id= db.Column(db.Integer, primary_key=True)
    french_name = db.Column(db.String(200), nullable=False)
    german_name = db.Column(db.String(200), nullable=False)
    quantity = db.Column(db.Integer, default=1)
    price = db.Column(db.Float, nullable=False)
    unit = db.Column(db.String(10), nullable=False)

    def __repr__(self):
        return '<Products %r>' % self.french_name

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/french")
def french():
    return render_template("french.html")

@app.route("/german")
def german():
    return render_template("german.html")

@app.route("/products", methods=['POST','GET'])
def products():
    if request.method =='POST':
        veri = request.form['veri']
        search = Products.query.filter_by(german_name=veri)
        return redirect(url_for('products.html',search=search))
    else:
        return render_template("products.html")

and this is my html:

<form action="/products" method="POST">
<input type="text" name="veri">
<input type="submit" value="Search">
</form>

<div>
    <table>
      {{search.name}}, {{ search.id }} , {{search.quantity}}

    </table>
</div>

{% endblock %}
2

There are 2 answers

0
Tekno Galaksi On BEST ANSWER
Products.query.filter_by(german_name=request.form.get('search')).all()

is the solution that i find

1
Vahid Kharazi On

Here is an example to access a form variable. Your question (access to a form input data) variable is not related to the database, models and other views that you've shared.

@app.route("/")
def index():
    return render_template("index.html")

@app.route('/products', methods=['POST'])
def products():
    veri = request.form['veri']
    print(veri)
    return f'here is veri: {veri}'

index.html

<form action="/products" method="POST">
<input type="text" name="veri">
<input type="submit" value="Search">
</form>