如何在烧瓶中生成动态网址? [英] How to generate dynamic urls in flask?

查看:25
本文介绍了如何在烧瓶中生成动态网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有几条记录,我想像这样形成 URL:

I have several records in the database which I Want to form URLs like so:

mysite.com/post/todays-post-will-be-about

todays-post-will-be-about 将从数据库中提取.

有什么办法可以把它放在烧瓶里吗?

Is there some way I could pull this off in flask?

推荐答案

您可以在 views.py 函数中放置变量名称.例如:

You can put variable names in your views.py functions. For example:

# 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)

为了让您的数据库信息显示在您的网站上,您需要将参数传递到模板中.因此,在您的模板中,您将引用这些参数,例如:

To get your database information to display on your site, you'll want to pass parameters into the template. So, in your template you'll reference those parameters like:

<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 页面处理,以防有人尝试手动输入帖子:

Then when you visit mysite.com/post/anything_here, the 'anything_here' will go into your function and be evaluated as necessary. You'll probably also want to set up 404 page handling, in case someone tries to enter a post manually:

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

这篇关于如何在烧瓶中生成动态网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆