您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Flask中生成动态网址?

如何在Flask中生成动态网址?

你可以将变量名放在views.py函数中。例如:

# you can also use a particular data type such as int,str
# @app.route('post/<int:id>', methods=['GET', 'POST'])
@app.route('post/<variable>', methods=['GET'])
def daily_post(variable):
    #do your code here
    return render_template("template.html",para1=meter1, para2=meter2)

@H_403_4@

为了使数据库信息显示在你的站点上,你需要将参数传递到模板中。因此,在模板中,你将引用以下参数:

<td>Post Author: {{ para1.author }}</td>
<td>Post Body: {{ para1.body }}</td>
<td>Date Posted: [{{ para2 }}] times</td>
@H_403_4@

然后,当你访问mysite.com/post/anything_here时,“ anything_here”将进入你的函数并根据需要进行评估。你可能还需要设置404页面处理,以防有人尝试手动输入帖子:

@app.errorhandler(404)
def not_found_error(error):
    return render_template('404.html', pic=pic), 404
@H_403_4@
Python 2022/1/1 18:17:31 有579人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶