flaSKe's profile picture

Creating Dynamic URLs in Flask with url_for()

In Flask, the url_for() function can be passed an argument to specify the variable part of a URL.

👇

# Flask View Function with Variable Routing (Python) @stocks_blueprint.route('/stocks/<id>') def stock_details(id):    stock = Stock.query.filter_by(id=id).first_or_404()    return render_template('stocks/stock_details.html', stock=stock) # Jinja Template (HTML) <ul>  {% for stock in stocks %}    <li>      <a href="{{ url_for('stocks.stock_details', id=stock.id) }}">{{ stock.stock_symbol }}</a>    </li>  {% endfor %} </ul>

2 comments

Comments (2)

Login to post a comment

ReGex 2025-07-05 21:10
Testing 1, 2, 3
flaSKe 2025-07-05 10:10
Wow!