mSapps
mSapps

Reputation: 601

flask url for fundtion is not working in case of pagination

I tried to load some javascript conditionally in my html file like following

{% if request.path == url_for('shipment') %}
<script>

    var enterprise = document.getElementById("enterprise_name");
    var destination = document.getElementById("destination");
    var route_code = document.getElementById("route_code");
    --------------------
    --------------------


</script>
{% endif %}

Above code block works , when there is no pagination . Like following

example.com/shipment

But it doesn't work in following scenario

 example.com/shipment/2

How can I make this work?

Upvotes: 0

Views: 39

Answers (1)

Isma&#239;l Mourtada
Isma&#239;l Mourtada

Reputation: 472

If you want to add a variable part to your shipment endpoint, you will have to call url_for with kwargs that will be transformed into URI parameters.

Example:

@app.route('/shipment')
def shipment():
    # -> url_for('shipment') will return '/shipment'

@app.route('/shipment/<shipment_number>')
def shipment_detail(shipment_number: int):
    # -> url_for('shipment_detail', shipment_number=2) will return '/shipment/2

=> Either you replace your == by the startswith method, or you write an elif loop taking care of the shipment detail URI

More information/examples on Flask URL building here

Upvotes: 1

Related Questions