Display links to new webpages created

后端 未结 1 1370
孤街浪徒
孤街浪徒 2021-01-14 06:18

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

相关标签:
1条回答
  • 2021-01-14 07:08

    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>
    
    0 讨论(0)
提交回复
热议问题