栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在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 pre here    return render_template("template.html",para1=meter1, para2=meter2)

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

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

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

@app.errorhandler(404)def not_found_error(error):    return render_template('404.html', pic=pic), 404


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号