Flask routing by username at top-level route

Is there a prefered way of setting up Flask so that it routes by username (mysite.com/<username>) at the top level but still works well (and fast) for all other routes and static files?

The way I imagine it now is something like:

@app.route('/<username>', methods=['GET'])
def username_route(username):

    if username_is_valid(username):  # DB checks, not too fast
        display_user_page(username)
    
    render_template('user_not_found.html')

But would that have any unwanted effects on other routes or static assets, favicons, or something that I’m forgetting?

Answer

You can send data with post request on frontend for clicking profile cases.

<button onclick="urlfor('username', {'user_id': id})"/>

Top level solution is possible with app.config.

app.config['RESERVED_ROUTES'] = [
    "faqs",
    "settings",
    "login",
    ...
]

Now we decide on request are user or reserved route. Because when we want to use blueprint in Flask it copy the config and give them an instance. So it is reachable for every request even you want to scale your app with blueprints.

@app.before_request
def decide_user_or_not():
    if request.path in app.config['RESERVED_ROUTES']:
        register_blueprint(route_blueprint)
    else:
        register_blueprint(user_blueprint)