Can Flask have optional URL parameters?

Another way is to write

@user.route('/<user_id>', defaults={'username': None})
@user.route('/<user_id>/<username>')
def show(user_id, username):
    pass

But I guess that you want to write a single route and mark username as optional? If that’s the case, I don’t think it’s possible.

Leave a Comment