Here is how I do it.
TEMPLATE
<p><a href="{{ url_for('delete_post', url=post.url) }}">delete</a></p>
View
# delete a post
@app.route('/<path:url>/d')
@login_required
def delete_post(url):
post = Post.get_post(url)
if post is None:
flash('post not found')
return redirect(url_for('home'))
db.session.delete(post)
db.session.commit()
Topic.update_counts()
flash('Your post has been deleted')
return redirect(url_for('index'))
the url_for is a Flask function that generates a url and binds it to a function.
Soooo.... in a nutshell.
- template generates the delete path using the url_for function.
- the delete function is called when you visit the page.
- message flashes to template saying delete complete.
to answer your original question....
if you simply want to pass a list of ids to your template....
First, create the display function.
@app.route('/index')
def index():
posts = Post.query.order_by(Post.pub_date.desc()) #SQLAlchemy query.
return render_template('index.html', posts=posts) #sends all posts to template.
Now in your template you can generate a list of post.ids.
{% for post in posts %}
{{post.id})
{% endfor %}
to add a delete link next to each item in your list use url_for.
{% for post in posts %}
<p>{{post.id}) <a href="{{ url_for('delete_post', id=post.id) }}">delete</a></p>
{% endfor %}
In this case I use post id instead of the post.url to identify the specific post I want to delete, so don't forget to modify your @app.route in your delete function.
@app.route('/<int:id>/delete')
Notice this solution does not require a form. You only need a form if your want to do bulk deletes with check boxes.