3

Let us say I create a blog, it has three static routes which are set up as usual. If none of the static routes match I want my method post_page() to return the answer by looking up the blog post in the database:

/                  → def index_page(): return "Index page"
/about             → def index_page(): return "About page"
/contact           → def index_page(): return "Contact page"
/<somethingelse>   → def post_page(): return get_content(somethingelse)

Some example URLs would be:

http://localhost/                                 → show the index page
http://localhost/about                            → show the about page
http://localhost/contact                          → show the contact page
http://localhost/the-largest-known-prime-number   → shows my log about the largest known prime number, it is fetched from the database.
http://localhost/my-cat                           → shows my log about my cat
http://localhost/my-dog                           → shows my log about my dog

What is the best way to do this using Flask? If it is possible I still want to be able to use url_for('about') to lookup the URLs of my static routes.

2 Answers 2

3

Simply define your routes with the static ones first, and it will work. The router will look for the best match and return it.

-> request comes in with /contact
|  /contact is in the routes
<- return call to contact function

-> request comes in with /foo-bar
|  /foo-bar matches the "postPage" route's regexp
<- return call to `postPage` function

Side note: see http://www.python.org/dev/peps/pep-0008/ about your function names (camel case is evil in Python).

3
  • Thanks for the answer! I'll update the function names. I was in Java-mode when I wrote them. ;-)
    – Deleted
    Jun 3, 2013 at 20:42
  • I don't have to define my static routes first, the routing seems to takes the most specific match. Your answer could be a bit clearer, if you clearly stated that the route defenition order doesn't matter.
    – Deleted
    Jun 3, 2013 at 20:55
  • 1
    @Kent - ah, I wasn't aware of that (I haven't used Flask for more than a year, though I am about to again). I thought it would simply find the first match in the set (even though I said "best match", I really was thinking "first match"). I will correct it (as well as add link to the docs) when I get a few more minutes.
    – orokusaki
    Jun 4, 2013 at 1:23
0

It seems like you want to use 1 view for both static and dynamic. using your example, it will be something like

static_page_list = ['about','contact']

@app.route('/')
@app.route('/<static_page>')
def hello(static_page=None):
    if static_page not in static_page_list:
        return get_content(somethingelse)
    else
        static_page_html = static_page + ".html"
        return render_template(static_page_html)

Now you can visit:

/

/about

/contact

/anything_else

1
  • The solution by orokusaki works well. I wanted the static URL to call its function and dynamic ones to call theirs. If I define things like he suggested it works as I want it. Your solution would not make url_for('.....') work, right?
    – Deleted
    Jun 4, 2013 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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