Flask:动态生成页面端点和内容

时间:2020-07-22 06:45:39

标签: python flask

我有一个这样的对象数组

   class post:
    def __init__(self, title, content, date, author):
        self.title = title
        self.content = content
        self.date = date
        self.author = author

posts = [
    post("Post1", "Post 1 content", str(date.today()), "Bob")
    post("Post2", "Post 2 content", str(date.today()), "Bob")
    post("Post3", "Post 3 content", str(date.today()), "Bob")
]

如何在mysite.com/Post1、mysite.com/Post2等处动态生成带有模板的页面?

1 个答案:

答案 0 :(得分:0)

由于您将帖子保留在列表中,因此可以使用

@app.route('/Post<int:number>')

使用索引(编号)从列表中获取帖子

@app.route('/Post<int:number>')
def post(number):
    if number < 1 or number > len(posts):
        abort(404, "This Post doesn't exist")   

    data = posts[number-1]

    return render_template_string('''
{{ item.title }}<br/>
{{ item.content }}<br/>
{{ item.date }}<br/>
{{ item.author }}<br/>''', item=data)

最低工作代码:

from flask import Flask, render_template_string
from datetime import date

class Post:
    def __init__(self, title, content, date, author):
        self.title = title
        self.content = content
        self.date = date
        self.author = author

posts = [
    Post("Post1", "Post 1 content", str(date.today()), "Bob"),
    Post("Post2", "Post 2 content", str(date.today()), "Bob"),
    Post("Post3", "Post 3 content", str(date.today()), "Bob"),
]

app = Flask(__name__)

@app.route('/')
def index():
    data=posts
    return render_template_string('''
{% for item in items %}
<a href="/{{ item.title }}">{{ item.title }}</a> OR 
<a href="{{ url_for('post', number=loop.index) }}">{{ item.title }}</a><br/>
{% endfor %}''', items=data)


@app.route('/Post<int:number>')
def post(number):
    if number < 1 or number > len(posts):
        abort(404, "This Post doesn't exist")   

    data = posts[number-1]

    return render_template_string('''
{{ item.title }}<br/>
{{ item.content }}<br/>
{{ item.date }}<br/>
{{ item.author }}<br/>''', item=data)


if __name__ == '__main__':
    app.run() #debug=True, use_reloader=False)

编辑:

如果您将帖子保留在字典中

posts = {
    "Post1": Post("Post1", "Post 1 content", str(date.today()), "Bob"),
    "Post2": Post("Post2", "Post 2 content", str(date.today()), "Bob"),
    "Post3": Post("Post3", "Post 3 content", str(date.today()), "Bob"),
}

然后您将使用

@app.route('/<string:title>')

使用标题/键从词典中获取帖子

@app.route('/<string:title>')
def post(title):
    if title not in posts:
         abort(404, "This Post doesn't exist")   
         
    data = posts[title]
    
    return render_template_string('''
{{ item.title }}<br/>
{{ item.content }}<br/>
{{ item.date }}<br/>
{{ item.author }}<br/>''', item=data)

最低工作代码:

from flask import Flask, render_template_string, abort
from datetime import date

class Post:
    def __init__(self, title, content, date, author):
        self.title = title
        self.content = content
        self.date = date
        self.author = author

posts = {
    "Post1": Post("Post1", "Post 1 content", str(date.today()), "Bob"),
    "Post2": Post("Post2", "Post 2 content", str(date.today()), "Bob"),
    "Post3": Post("Post3", "Post 3 content", str(date.today()), "Bob"),
}

app = Flask(__name__)

@app.route('/')
def index():
    data = posts
    return render_template_string('''
{% for key, item in items.items() %}
<a href="/{{ item.title }}">{{ item.title }}</a> OR 
<a href="{{ url_for('post', title=item.title) }}">{{ item.title }}</a><br/>
{% endfor %}''', items=data)

@app.route('/<string:title>')
def post(title):
    if title not in posts:
         abort(404, "This Post doesn't exist")   
         
    data = posts[title]
    
    return render_template_string('''
{{ item.title }}<br/>
{{ item.content }}<br/>
{{ item.date }}<br/>
{{ item.author }}<br/>''', item=data)


if __name__ == '__main__':
    app.run() #debug=True, use_reloader=False)