简体   繁体   中英

How do I get rid of a post from a flask blog?

I made a blog and want to get rid of a blog post thought the front end. And I have some trouble with it and getting the error

"sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.int' is not mapped"

I think that the view is messed up but i am not too sure. Would love any feedback, Thanks!!

home.html
<span class="pull-right"><a class="text-danger" href="{{ url_for('delete_post', posts_id=posts.id) }}">[delete]</a></span>

views
@app.route('/delete_post/<int:posts_id>/', methods=('GET', 'POST'))
def delete_post(posts_id):
    posts = Blogpost.query.filter_by(id=posts_id).first_or_404()
    db.session.delete(posts_id)
    db.session.commit()
    return redirect(url_for('home'))

models
class Blogpost(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50))
author = db.Column(db.String(20))
date_posted = db.Column(db.DateTime)
content = db.Column(db.Text)

您需要将帖子查询(而不是ID)传递给delete方法。

db.session.delete(posts)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM