Flask is my go-to framework for any Python web application that I want to built. This is due to its relative ease of setup as well as its file structure being easy to navigate. Once Flask has been set up, it is simple to create a new Flask app and get started.

One of the best things about Flask are the cool tricks you can do with URLs. Generally, a URL route will be created using:

@app.route('/')
def index():
    return 'This is the homepage'
@app.route('/login')
def login():
    return 'Login page'

This is the basic way to create a URL route. However, these URL routes can also take in parameters which makes them very flexible. For example, if you want a page that shows results for individual user profiles:

@app.route('/users/<username>')
def user():
    #Code for getting data from database based on username
    return username

This makes it quick and easy to generate links for pages without having to make each one. Instead of having to have separate pages for each user, instead you can have a template for user pages which gets filled with a specific user’s information. It’s easy to redirect to generated pages like this as well using url_for():

return redirect(url_for('user', username='admin'))

This will redirect someone to a user page for a username called ‘admin’. By combining these two tips, you can create applications that accept information and process it from a form before redirecting the user to a page that displays the information based on what was passed in. There are many applications for this like submitting something to a database and then redirecting to a page that pulls the data from the database to display using a in the generated URL. Hopefully this will help you create more powerful Flask applications that can better utilize built-in URL functions!

This was crossposted from my blog at Medium.com