Python没有从SQLAlchemy数据库中删除具有给定文章标题的正确文章
我试图从flask中的SQLAlchemy数据库中删除一篇文章,方法是使用该文章的标题抓取该文章 然而,它似乎是在删除post表中的第一行,而不是删除我打算删除的实际post 以下是删除帖子的路线:Python没有从SQLAlchemy数据库中删除具有给定文章标题的正确文章,python,html,flask,Python,Html,Flask,我试图从flask中的SQLAlchemy数据库中删除一篇文章,方法是使用该文章的标题抓取该文章 然而,它似乎是在删除post表中的第一行,而不是删除我打算删除的实际post 以下是删除帖子的路线: @post.route('/admin_delete_post/<post_title>', methods=['POST']) def delete_post(post_title): from mysite import db from mysite.models i
@post.route('/admin_delete_post/<post_title>', methods=['POST'])
def delete_post(post_title):
from mysite import db
from mysite.models import Post
post = Post.query.filter_by(title=post_title).first()
if post:
db.session.delete(post)
db.session.commit()
flash('Post has been deleted successfuly!', 'danger')
return redirect(url_for('main.home'))
else:
abort(404)
@post.route('/admin\u delete\u post/',方法=['post'])
def delete_post(post_标题):
从mysite导入数据库
从mysite.models导入帖子
post=post.query.filter\u by(title=post\u title).first()
如果发布:
db.session.delete(post)
db.session.commit()
flash(“帖子已成功删除!”,“危险”)
返回重定向(url_for('main.home'))
其他:
中止(404)
以下是html代码:
{% extends 'layout.html' %}
{% block content %}
{% for post in posts %}
{% if current_user.is_authenticated %}
..............
..............
<!-- Button trigger modal -->
<button style="display: inline; float: left; margin-right: 10px;" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
Delete this post
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Warning!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure to delete this post?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
<form style="float: right; margin-left: 10px;" action="{{ url_for('post.delete_post', post_title=post.title) }}" method="post">
<input class="btn btn-danger" type="submit" value="Delete">
</form>
</div>
</div>
</div>
</div>
<a href="{{ url_for('post.edit_post', post_title=post.title) }}">
<button style="display: inline; float: left;" type="button" class="btn btn-primary">
Edit this post
</button>
</a>
</div>
<!-- Blog Post End -->
{% endfor %}
{%extends'layout.html%}
{%block content%}
{posts%%中的post为%s}
{%如果当前用户是经过身份验证的%}
..............
..............
删除此帖子
警告
&时代;
你确定要删除此帖子吗?
不
{%endfor%}
你能从你的mysite上发布代码吗?在我看来,这一行动似乎发生在那里。因此,为了回答您的问题,我们需要查看它。首先,您可以尝试向端点发送HTTP POST请求,看看会发生什么。如果您在linux上,您可以使用curl
。此外,您能否打印出变量post\u title
。我怀疑这不是一个字符串,而是一个字典。非常感谢。正如您在delete_post路由中所看到的,除了上面的HTML代码中已经嵌入的表单之外,它没有表单,而HTML代码负责删除操作。。。