显示创建新网页的链接 [英] Display links to new webpages created
问题描述
我正在用Python构建一个网站(使用heroku),我想创建一个最新的提交部分。也就是说,当我在我的Python应用程序中创建一个新的 @ app.route(blah)
时,我想要一个到新页面的链接显示在最新提交部分在我的主页上。
编辑:这是我的代码
import os
从flask导入导入json
从werkzeug.routing导入Flask,render_template,url_for
导入Map,Rule,NotFound,RequestRedirect, BaseConverter
$ b $ app = Flask(__ name__)
$ b @ 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 = []
规则在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__':
#绑定到PORT,否则默认为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>链接< / title>
< / head>
< body>
< ul>
{%为网址,链接中的端点%}
< li>< a href ={{url}}> {{endpoint}}< / a>< / li> ;
{%endfor%}
< / ul>
< / body>
< / html>
应用程序的所有路由都存储在 app.url_map
这是一个 werkzeug.routing.Map
。这就是说,你可以遍历
Rule
实例,方法是使用
iter_rules
方法:
from flask import Flask,render_template,url_for
$ b $ app = Flask(__ name__)
@ app.route(/ all-links)
def all_links():
links = []
作为app.url_map.iter_rules()中的规则:
if len(rule.defaults)> = len(rule.arguments):
url = url_for(rule.endpoint,** (rule.defaults或{}))
links.append((url,rule.endpoint))
return render_template(all_links.html,links = links)
$ c
$ b $ p $ {#all_links .html#}
< ul>
{%为网址,链接中的端点%}
< li>< a href ={{url}}> {{endpoint}}< / a>< / li> ;
{%endfor%}
< / ul>
I'm building a website with Python (using heroku) and I would like to create a "newest submissions" section. That is, when I create a new @app.route(blah)
in my Python app, I want a link to the new page to show up under the "newest submissions" section on my homepage.
Is this possible?
EDIT: here's my code
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)
and the all_links.html file
<!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>
All the routes for an application are stored on app.url_map
which is an instance of werkzeug.routing.Map
. That being said, you can iterate over the Rule
instances by using the iter_rules
method:
from flask import Flask, render_template, url_for
app = Flask(__name__)
@app.route("/all-links")
def all_links():
links = []
for rule in app.url_map.iter_rules():
if len(rule.defaults) >= len(rule.arguments):
url = url_for(rule.endpoint, **(rule.defaults or {}))
links.append((url, rule.endpoint))
return render_template("all_links.html", links=links)
{# all_links.html #}
<ul>
{% for url, endpoint in links %}
<li><a href="{{ url }}">{{ endpoint }}</a></li>
{% endfor %}
</ul>
这篇关于显示创建新网页的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!