3

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>
  • 1
    `Is this possible?` Yes. You'll probably get better responses if you post some code for what you have already tried. – John Oct 31 '12 at 04:17
  • At this point, I'm looking more for some direction, than an actual "here is the answer" answer. Can i do this completely in flask? Do i use a combination of python coding and flask coding in my main app.py file? – Cross Game Chat Oct 31 '12 at 04:20

1 Answers1

10

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>
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • so, the for loop in the code you posted takes all the routes in the app.url_map and puts them in the links list. But what is the point of render_template("all_links.html", links=links)? Doesn't that make it so that some page located at herokuapp.com/all-links returns the all_links.html when someone goes there? – Cross Game Chat Oct 31 '12 at 22:31
  • @CrossGameChat - the last line is just an example - I'll add an example Jinja template for the `all_links` template. – Sean Vieira Nov 01 '12 at 01:55
  • Thanks a lot for the help! I'm still getting a 500 internal server error "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application." I updated the post with the code for my app.py and all_links.html – Cross Game Chat Nov 01 '12 at 06:21
  • @CrossGameChat - the example I gave you was missing the key word - `return`. Add `return` to `render_template` and give it another try. I've updated my example as well. – Sean Vieira Nov 01 '12 at 13:09
  • Again, thanks a lot for the help. I'm still getting the error (even tried recopying your code). I'll show the code to one of my professors and see if they can figure out what is wrong. It's a little hard for me to understand the flask and werkzeug documentation. – Cross Game Chat Nov 01 '12 at 19:35
  • @CrossGameChat - you hadn't imported `url_for` from Flask in your example - I've updated my code with a working version. – Sean Vieira Nov 01 '12 at 20:18
  • I get an error too when it gets to the rule for static content, it cannot get url_for(rule.endpoint) on it. It raises a BuildError(endpoint, values, method) exception. I'll be trying to find a work around... – Benjamin Chodroff Nov 19 '12 at 17:30
  • @CrossGameChat - you need to make sure that your rule has all its arguments filled. I've updated my example. – Sean Vieira Nov 19 '12 at 17:49