H
H
hey_umbrella2021-08-22 23:49:24
Flask
hey_umbrella, 2021-08-22 23:49:24

How to pass id via html to flask?

The question is probably very stupid)

cursor = con.execute(f"SELECT name,age,imagepath,gender,phone,timeadded,id,description,city FROM pets WHERE userid = '{current_user.id}' ")

I pass this data from the database to flask, and then I show it in html.
{% for item in items %}
      <div class="card card-item-01">
      <div class="card__image-wrapper">
      <img class="card__image" src="/static{{item[2]}}" alt="Desert" width="280" height="420">
      </div>
      <form method = "POST">
        <div class="card__content">
          <input type="text" placeholder="{{item[3]}}" name="gender">
          <input type="text" placeholder="{{item[0]}}" name="name">
          <input type="text" placeholder="{{item[1]}}" name="age">
          <input type="text" placeholder="{{item[7]}}" name="desc">
          <input type="text" placeholder="{{item[8]}}" name="city">
          <input type="text" placeholder="{{item[4]}}" name="phone">
          <input type="file" placeholder="" name="imagepath">
          <input type="submit" name="id" value="Сохранить">
        </div>  
      </form>
    </div>
    {% endfor %}


I output all this data, but how do I pass the id of the object, because each row in the database has an id and I pass it, after the person changes the data, how do I pass the id back to flask, because I can’t do it differently SELECT exactly to this line, I hope I explained it clearly, thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gleb, 2021-08-23
@Hrafnir

You need to add a page to the form for which you will write an endpoint, for example:

<form method="POST" action="{{ url_for('main.post_id') }}" >

At the same time, do not forget to send your form object to the html template, ala form=Form()
In the view itself, you need to get this value:
@main.post_id('/id')
   def post_id():
        form = Form()
        ....
        return render_template('template.html', form=form)

and already twirl its attributes
In general, you should read the documentation in WTForms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question