-1

I am trying to creating dynamic url in html and routing all "orders/<key_id[0]>" to flaks app.

On the browser hyperlink should be link to name but it should route as "orders/<key_id[0]>"

Tried a lot of thing couln't manage to generate href with key_id variable with it.

Trying to create something like

<a href="orders + /key_id">{{key_id[0]}<p>{{name[0]}}</p></a></td>

My Base Html Code:

{% for name, sample, date, coord_x, coord_y, key_id in zipped_ %}
                    <tr>
                      <td><a href="orders"><p>{{name[0]}}</p></a></td>
                      <td>{{sample[0]}}</td>
                      <td><span class="badge badge-danger">{{date[0]}}</span></td>
                      <td>
                        <div class="sparkbar" data-color="#00a65a" data-height="20">{{coord_x[0]}},{{coord_y[0]}}</div>
                      </td>
                    </tr>  
                  {% endfor %}

Flask App Routing:

@views.route('/orders')
@views.route('/orders/<key_id>', methods=['GET', 'POST']) # ordersda birşey yazdın ama indexten bir post ya da get gelmiyor sanki
def orders():
    print(key_id)
    #mycursor.execute("SELECT * FROM processed_data WHERE primary_key IN (%s)",(key_id))
    #zip_q = mycursor.fetchall()
    

    return render_template("orders.html", zip_q=zip_q)  
Mindtrip
  • 33
  • 7

2 Answers2

0

There are many ways to do it depending on your Jinja version. The "most compatible" one is to use string formatting with urlencode filter

<a href="{{ 'orders/%s'%key_id|urlencode }}">{{key_id[0]}<p>{{name[0]}}</p></a>

Ismaïl Mourtada
  • 452
  • 5
  • 11
0

I have found the answer at last.

<a href= {{ url_for('views.orders' , key_id=key_id[0]) }} <p>{{name[0]}}</p></a></td>

with url_for i can dynamically edit the link and for passing variable to flask:

@views.route('/orders')
@views.route('/orders/<key_id>', methods=['GET', 'POST'])
def orders(key_id):
print(key_id)

i can get the key_id variable in the flask app.

Mindtrip
  • 33
  • 7