2

If I'm making a blog site and I want to set up routing such that

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

routes to the post with name 'postname' of the user with name 'username', how do I get the html to recognize this? I've been trying to do something like

<a href={{ url_for('/', username=user.name, postname=post.name) }}>{{post.name}}</a>

I'm also trying to reconcile this with Flask understanding special keywords /login or /about so that it checks if the user is trying to access those first. How can I implement those checks?

1 Answer 1

4

The first argument to url_for in your template should be the name of the view function you decorated:

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

Now, you can write this in your template:

{{ url_for('view_user_post', username=user.name, postname=post.name) }}

This lets you change the URL in the route without having to update it elsewhere in your codebase.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.