Flask url_for 停止接受两位数的用户 ID。漏洞?
Flask url_for stop accepting double-digit user IDs. BUG?
我对 Flask 比较陌生,我无法弄清楚我的函数有什么问题,也找不到有人遇到过同样的问题。甚至难以解释。
模板url_forlink:
<a href="{{ url_for('view_post', pc=post.product_category_by.product_cat_name, post_id=post.id, ui=post.author_id) }}">
Flask视图函数:(Flask SQLAlchemy.PostgreSQL)
@app.route('/viewref/<pc>/<int:post_id><int:ui>')
def view_post(pc, post_id, ui):
post = db.session.query(Post).filter_by(id=post_id).first()
db.session.commit()
gc.collect()
return render_template('view_post.html', post=post)
示例:
让我们拿一个 ID 为 26 的 post 和两个 ID 为 9[=55 的用户(或 post 的作者) =] 和 10。当我传递 ID 为 9 的用户时,我被重定向到结尾为 url 的正确视图:/viewref/productCat/269
(26 是 post id 而 9 是用户 id)。太好了,它起作用了,我看到 post!
问题:
但是当 ID 10 或更高(12、299 等)的用户在 url_for
中传递时,view_post
函数的查询从数据库中提取 None
,随后我的 view_post.html 模板抛出错误
(UndefinedError: 'None' has no attribute 'product_category_by')
有什么联系?
view_post
函数的关键是只通过给定的post_id
取一个post。为什么在用户 ID > 9 的情况下它会得到 None
,如果我的函数仅使用 post_id 进行查询,而其他变量仅用于 url 构造?
P.S。如果我从 url_for
和 view_post
函数中排除 ui
一切正常。
我不确定您希望 URL 路由器如何判断 post_id
结束和 ui
开始的位置。您应该使用实际的分隔符:
@app.route('/viewref/<pc>/<int:post_id>/<int:ui>')
我认为你应该改变你的 URL 方案。如果用户 id 是 12 而 post_id 是 34 怎么办?在您的 URL 中,它将连接到 1234。我们如何区分 post_id: 1, user_id: 234 与 post_id: 123, user_id : 4?
我对 Flask 比较陌生,我无法弄清楚我的函数有什么问题,也找不到有人遇到过同样的问题。甚至难以解释。
模板url_forlink:
<a href="{{ url_for('view_post', pc=post.product_category_by.product_cat_name, post_id=post.id, ui=post.author_id) }}">
Flask视图函数:(Flask SQLAlchemy.PostgreSQL)
@app.route('/viewref/<pc>/<int:post_id><int:ui>')
def view_post(pc, post_id, ui):
post = db.session.query(Post).filter_by(id=post_id).first()
db.session.commit()
gc.collect()
return render_template('view_post.html', post=post)
示例:
让我们拿一个 ID 为 26 的 post 和两个 ID 为 9[=55 的用户(或 post 的作者) =] 和 10。当我传递 ID 为 9 的用户时,我被重定向到结尾为 url 的正确视图:/viewref/productCat/269
(26 是 post id 而 9 是用户 id)。太好了,它起作用了,我看到 post!
问题:
但是当 ID 10 或更高(12、299 等)的用户在 url_for
中传递时,view_post
函数的查询从数据库中提取 None
,随后我的 view_post.html 模板抛出错误
(UndefinedError: 'None' has no attribute 'product_category_by')
有什么联系?
view_post
函数的关键是只通过给定的post_id
取一个post。为什么在用户 ID > 9 的情况下它会得到 None
,如果我的函数仅使用 post_id 进行查询,而其他变量仅用于 url 构造?
P.S。如果我从 url_for
和 view_post
函数中排除 ui
一切正常。
我不确定您希望 URL 路由器如何判断 post_id
结束和 ui
开始的位置。您应该使用实际的分隔符:
@app.route('/viewref/<pc>/<int:post_id>/<int:ui>')
我认为你应该改变你的 URL 方案。如果用户 id 是 12 而 post_id 是 34 怎么办?在您的 URL 中,它将连接到 1234。我们如何区分 post_id: 1, user_id: 234 与 post_id: 123, user_id : 4?