TheLinuxPro
TheLinuxPro

Reputation: 55

How to deal with many routes in flask

in flask we have the usual

@app.route()
we all know about that but what if you have many routes (lets say 60) in theory how would you organize them? a single views.py file wont cut it and organizing them by category wont cut it either because we will have 8 main pages and 52 user posts so how would you deal with such a problem?
NOTE: this is only a theory so don't say that i am stupid

Upvotes: 2

Views: 2614

Answers (2)

marzique
marzique

Reputation: 694

For '52 user posts' and similar page routes you should use routes with variables, like so

@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id

I hope this will make it clear for you

Upvotes: 2

Michael Spagon
Michael Spagon

Reputation: 151

I highly recommend checking out an article called Structure of a Flask Project taken from this Python Bytes podcast episode.

It talks about a functional based structure vs an app based structure (which you commonly find in Django). These are two approaches you can take. These are just recommendations and Flask is very flexible so you can do whatever you want.

Upvotes: 3

Related Questions