【发布时间】:2012-10-31 04:11:33
【问题描述】:
我正在使用 Python(使用 heroku)构建一个网站,我想创建一个“最新提交”部分。也就是说,当我在我的 Python 应用程序中创建一个新的@app.route(blah) 时,我希望一个指向新页面的链接显示在我主页的“最新提交”部分下。
这可能吗?
编辑:这是我的代码
import os
import json
from flask import Flask, render_template, url_for
from werkzeug.routing import Map, Rule, NotFound, RequestRedirect, BaseConverter
app = Flask(__name__)
@app.route('/')
def index():
return render_template('welcome.html')
@app.route('/about', endpoint='about')
def index():
return render_template('about.html')
@app.route('/contact', endpoint='contact')
def index():
return render_template('contact.html')
@app.route('/all-links', endpoint='all-links')
def all_links():
links = []
for rule in app.url_map.iter_rules():
url = url_for(rule.endpoint)
links.append((url, rule.endpoint))
return render_template('all_links.html', links=links)
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
还有 all_links.html 文件
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>links</title>
</head>
<body>
<ul>
{% for url, endpoint in links %}
<li><a href="{{ url }}">{{ endpoint }}</a></li>
{% endfor %}
</ul>
</body>
</html>
【问题讨论】:
-
Is this possible?是的。如果您发布一些您已经尝试过的代码,您可能会得到更好的响应。 -
在这一点上,我正在寻找更多的方向,而不是实际的“这就是答案”的答案。我可以在烧瓶中完全做到这一点吗?我是否在我的主 app.py 文件中使用了 python 编码和烧瓶编码的组合?