Home > OS >  How do I pass a Flask table cell value to python function?
How do I pass a Flask table cell value to python function?

Time:10-15

I have this table in Flask:

<tbody>
   {% for record in records %}
   <tr>
      <td onclick=search_results()>{{ record.id }}</td>
      <td>{{ record.contractor }}</td>
      <td>{{ record.category }}</td>
      <td><A HREF='tel:{{ record.phone }}'>{{ record.phone }}</A></td>
   </tr>
   {% endfor %}
</tbody>

And this function:

@app.route('/detail/<id>')
def search_results(id):
    records = db.session.query(Feedback).filter(Feedback.id == id)
    print('search')
    for record in records:
        print(record)
    return render_template('pageDetail.html', records=records)

I have researched similar questions but struggling still to understand how I can pass the id number of the selected cell to the search_results function.

Thank you in advance!

CodePudding user response:

You need to hit the endpoint from the html and not the function call, try doing this,

<tbody>
   {% for record in records %}
   <tr>
      <td href='/detail/{{ record.id }}'>{{ record.id }}</td>
      <td>{{ record.contractor }}</td>
      <td>{{ record.category }}</td>
      <td><A HREF='tel:{{ record.phone }}'>{{ record.phone }}</A></td>
   </tr>
   {% endfor %}
</tbody>

Your flask view is fine.

CodePudding user response:

No Method has been specified for flask? If not used then why not simply use a python function.

refer this example:

@app.route('/classify', methods=['GET','POST'])
# these requests trigger the getresponse function where data can be retrieved
def getresponse():
    if request.method == 'GET':
        return(render_template('main.html'))

    if request.method == 'POST':
        extracted_title = request.form['string']

try:

@app.route('/detail/<id>', methods=['GET'])
def search_results(id):
    records = db.session.query(Feedback).filter(Feedback.id == id)
    print('search')
    for record in records:
        print(record)
    return render_template('pageDetail.html', records=records)
  • Related