Flask : url_for with <a> tag doesn't get rendered in browser
Question:
here is my template code (menu.html) :
<html>
<body>
<h1>{{restaurant.name}}</h1>
{% for i in items %}
<div>
<p>{{i.name}}</p>
<p>{{i.description}}</p>
<p> {{i.price}} </p>
<a href='{{url_for('editMenuItem', restaurant_id = restaurant.id, menu_id = i.id) }}'>Edit</a>
</br>
<a href = '{{url_for('deleteMenuItem', restaurant_id = restaurant.id, menu_id = i.id ) }}'>Delete</a>
</div>
{% endfor %}
</body>
</html>
And here is my code where I invoke the render_template()
function :
@app.route('/')
@app.route('/restaurants/<int:restaurant_id>/')
def restaurantMenu(restaurant_id):
restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
items = session.query(MenuItem).filter_by(restaurant_id=restaurant.id)
return render_template('menu.html',restaurant=restaurant,items=items)
The import :
from flask import Flask, render_template, url_for
My problem is that my Edit
and Delete
anchor tags don’t get rendered at all! Why is that so ?
EDIT:1
In fact whatever changes I make in the menu.html
do not get changed at all ! Even if it as simple as adding a static text into it.
Answers:
Could you try this code in template?
<a href='{{url_for('editMenuItem', restaurant_id=restaurant.id, menu_id=items.id) }}'>Edit</a>
</br>
<a href = '{{url_for('deleteMenuItem', restaurant_id=restaurant.id, menu_id=items.id) }}'>Delete</a>
Instead of providing i
(i.id
) in the template, there should be items
(items.id
), because that is the name of parameter in backend code (render_template('menu.html',restaurant=restaurant,items=items)
).
About all the changes in template, maybe you need to clear cache or use Ctrl + Shift + R
keyboard combination?
Hope it helps.
Your problem is that you are trying to use single-quotes inside a single-quoted string. You might be able to get around that by using backslash-escaped quotes: '
. But a simpler way is to just change the outer quotes to double-quotes, i.e., change
<a href='{{url_for('editMenuItem', restaurant_id = restaurant.id,
menu_id = i.id) }}'>Edit</a>
to
<a href="{{url_for('editMenuItem', restaurant_id = restaurant.id,
menu_id = i.id) }}">Edit</a>
here is my template code (menu.html) :
<html>
<body>
<h1>{{restaurant.name}}</h1>
{% for i in items %}
<div>
<p>{{i.name}}</p>
<p>{{i.description}}</p>
<p> {{i.price}} </p>
<a href='{{url_for('editMenuItem', restaurant_id = restaurant.id, menu_id = i.id) }}'>Edit</a>
</br>
<a href = '{{url_for('deleteMenuItem', restaurant_id = restaurant.id, menu_id = i.id ) }}'>Delete</a>
</div>
{% endfor %}
</body>
</html>
And here is my code where I invoke the render_template()
function :
@app.route('/')
@app.route('/restaurants/<int:restaurant_id>/')
def restaurantMenu(restaurant_id):
restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
items = session.query(MenuItem).filter_by(restaurant_id=restaurant.id)
return render_template('menu.html',restaurant=restaurant,items=items)
The import :
from flask import Flask, render_template, url_for
My problem is that my Edit
and Delete
anchor tags don’t get rendered at all! Why is that so ?
EDIT:1
In fact whatever changes I make in the menu.html
do not get changed at all ! Even if it as simple as adding a static text into it.
Could you try this code in template?
<a href='{{url_for('editMenuItem', restaurant_id=restaurant.id, menu_id=items.id) }}'>Edit</a>
</br>
<a href = '{{url_for('deleteMenuItem', restaurant_id=restaurant.id, menu_id=items.id) }}'>Delete</a>
Instead of providing i
(i.id
) in the template, there should be items
(items.id
), because that is the name of parameter in backend code (render_template('menu.html',restaurant=restaurant,items=items)
).
About all the changes in template, maybe you need to clear cache or use Ctrl + Shift + R
keyboard combination?
Hope it helps.
Your problem is that you are trying to use single-quotes inside a single-quoted string. You might be able to get around that by using backslash-escaped quotes: '
. But a simpler way is to just change the outer quotes to double-quotes, i.e., change
<a href='{{url_for('editMenuItem', restaurant_id = restaurant.id,
menu_id = i.id) }}'>Edit</a>
to
<a href="{{url_for('editMenuItem', restaurant_id = restaurant.id,
menu_id = i.id) }}">Edit</a>