从流中删除 Post
Deleting a Post from a Stream
我是创建 Web 应用程序的新手,因此我决定从学习 Flask 开始。我正在制作一个社交网络作为一个副项目来练习和学习一些基本技能。
到目前为止,我已经能够构建一个用户可以 post 访问的 "Stream",有点像 Twitter。 我一直在努力寻找一种允许用户删除 posts 的方法。 我正在使用带有 Peewee 库的 SQLite 数据库。这是我尝试过的:
@app.route('/delete_post/<int:post_id>')
@login_required
def delete_post(post_id):
delete = models.Post.select().where(models.Post.id == post_id)
try:
models.DATABASE.delete(delete)
except models.DoesNotExist:
abort(404)
else:
flash("This post has successfully been deleted.", "success")
return redirect(url_for('stream', stream = stream))
此外,下面是 Post 的创建方式:
class Post(Model):
timestamp = DateTimeField(default=datetime.datetime.now())
user = ForeignKeyField(
rel_model= User,
related_name='posts'
)
content = TextField()
class Meta:
database = DATABASE
order_by = ('-timestamp',)
最后,有一个模板,当用户选择 "Delete"
时,delete_post 方法将被调用
{% extends "base.html" %}
{% block content %}
{% for post in stream %}
<article>
<h2>
<a href="{{ url_for('stream', username=post.user.username) }}">{{ post.user.username }}</a>
</h2>
<i class="clock"></i>
<time>
{{ post.timestamp.strftime("%a, %d %b %Y %H:%M") }}
</time>
<a href="{{ url_for('view_post', post_id=post.id) }}" class="view">View</a>
<a href="{{ url_for('delete_post', post_id=post.id) }}" class="view" >| Delete</a>
<div class="post">
{{ post.content }}
</div>
</article>
{% endfor %}
{% endblock %}
如何删除 post? 当我尝试我的解决方案时,我得到:AttributeError: 'SqliteDatabase' object has no attribute 'delete'
我知道这个问题很啰嗦,但任何帮助将不胜感激,并将在未来的项目中帮助我很多。如果我需要澄清任何其他问题,请告诉我。
谢谢!
要删除:
def delete_post(post_id):
try:
post = models.Post.select().where(models.Post.id == post_id).get()
except models.Post.DoesNotExist:
abort(404)
post.delete_instance()
flash("This post has successfully been deleted.", "success")
return redirect(url_for('stream', stream = stream))
或者,您可以这样写:
def delete_post(post_id):
post = models.Post.delete().where(models.Post.id == post_id).execute()
flash("This post has successfully been deleted.", "success")
return redirect(url_for('stream', stream = stream))
编辑:我还想向您指出涵盖各种主题的文档...http://docs.peewee-orm.com/en/latest/peewee/querying.html#deleting-records
我是创建 Web 应用程序的新手,因此我决定从学习 Flask 开始。我正在制作一个社交网络作为一个副项目来练习和学习一些基本技能。
到目前为止,我已经能够构建一个用户可以 post 访问的 "Stream",有点像 Twitter。 我一直在努力寻找一种允许用户删除 posts 的方法。 我正在使用带有 Peewee 库的 SQLite 数据库。这是我尝试过的:
@app.route('/delete_post/<int:post_id>')
@login_required
def delete_post(post_id):
delete = models.Post.select().where(models.Post.id == post_id)
try:
models.DATABASE.delete(delete)
except models.DoesNotExist:
abort(404)
else:
flash("This post has successfully been deleted.", "success")
return redirect(url_for('stream', stream = stream))
此外,下面是 Post 的创建方式:
class Post(Model):
timestamp = DateTimeField(default=datetime.datetime.now())
user = ForeignKeyField(
rel_model= User,
related_name='posts'
)
content = TextField()
class Meta:
database = DATABASE
order_by = ('-timestamp',)
最后,有一个模板,当用户选择 "Delete"
时,delete_post 方法将被调用{% extends "base.html" %}
{% block content %}
{% for post in stream %}
<article>
<h2>
<a href="{{ url_for('stream', username=post.user.username) }}">{{ post.user.username }}</a>
</h2>
<i class="clock"></i>
<time>
{{ post.timestamp.strftime("%a, %d %b %Y %H:%M") }}
</time>
<a href="{{ url_for('view_post', post_id=post.id) }}" class="view">View</a>
<a href="{{ url_for('delete_post', post_id=post.id) }}" class="view" >| Delete</a>
<div class="post">
{{ post.content }}
</div>
</article>
{% endfor %}
{% endblock %}
如何删除 post? 当我尝试我的解决方案时,我得到:AttributeError: 'SqliteDatabase' object has no attribute 'delete'
我知道这个问题很啰嗦,但任何帮助将不胜感激,并将在未来的项目中帮助我很多。如果我需要澄清任何其他问题,请告诉我。
谢谢!
要删除:
def delete_post(post_id):
try:
post = models.Post.select().where(models.Post.id == post_id).get()
except models.Post.DoesNotExist:
abort(404)
post.delete_instance()
flash("This post has successfully been deleted.", "success")
return redirect(url_for('stream', stream = stream))
或者,您可以这样写:
def delete_post(post_id):
post = models.Post.delete().where(models.Post.id == post_id).execute()
flash("This post has successfully been deleted.", "success")
return redirect(url_for('stream', stream = stream))
编辑:我还想向您指出涵盖各种主题的文档...http://docs.peewee-orm.com/en/latest/peewee/querying.html#deleting-records