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>
2
  • 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, 2012 at 4: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? Oct 31, 2012 at 4:20

1 Answer 1

9

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>
8
  • 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? Oct 31, 2012 at 22:31
  • @CrossGameChat - the last line is just an example - I'll add an example Jinja template for the all_links template. Nov 1, 2012 at 1: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 Nov 1, 2012 at 6: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. Nov 1, 2012 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. Nov 1, 2012 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.