python - Flask 使用 id 来个性化登陆页面
问题描述
我正在完成烧瓶教程。我正在尝试修改教程代码以更好地理解它。下面是主页的代码。它访问人们在博客上写的所有帖子。
@bp.route('/')
def index():
db = get_db()
posts = db.execute(
'SELECT p.id, title, body, created, author_id, username'
' FROM post p JOIN user u ON p.author_id = u.id'
' ORDER BY created DESC'
).fetchall()
return render_template('blog/index.html', posts=posts)
我试图调整它,使它只显示该用户写的帖子。作为一个起点,我尝试将 id 作为输入变量传递给 index() 函数,就像函数 get_post() 所做的一样(见下文)
@bp.route('/create', methods=('GET', 'POST'))
@login_required
...
def get_post(id, check_author=True):
post = get_db().execute(
'SELECT p.id, title, body, created, author_id, username'
' FROM post p JOIN user u ON p.author_id = u.id'
' WHERE p.id = ?',
(id,)
).fetchone()
if post is None:
abort(404, "Post id {0} doesn't exist.".format(id))
if check_author and post['author_id'] != g.user['id']:
abort(403)
return post
但是,这会引发一个错误,其中 id 未被识别为输入变量。当我添加@login_required 时会发生这种情况,并且当我尝试将 ip 地址更改为“/int:id//”时,该地址在另一个以 id 作为输入的函数中使用。
谢谢您的帮助:))
解决方案
推荐阅读
- reactjs - 如何让等待在 React 中与 redux Saga 一起工作?
- vue.js - 模拟数据关闭时如何向服务器写入发布请求?
- arrays - Ruby FFI to call C function with array of arrays
- docker - How to accept the license agreement when building rti-connext-dds-5.3.1 with docker build?
- javascript - 我需要在按下相等按钮时计算 setState 中的两个值
- arrays - Accumulating data based on two criteria
- python - Dataframe 用 1 和 0 替换所有 True 和 False 值
- android - 如何将铃声模式完全设置为静音?
- r - 世界地图:按“子区域”过滤会删除许多区域
- python - 使用 lambda 作为函数和使用 lambda 和 map 有什么区别吗?