H
H
hardwellZero2015-03-07 17:29:37
Flask
hardwellZero, 2015-03-07 17:29:37

How to generate a link to a post?

Hello.
How can I generate a link for each blog post?
example.com/posts/*
Where * is from 1 to the number of posts.
I found this method:
@app.route('/post/<post.id>')
But I did not understand how to fasten it. The console swears.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2015-03-07
@hardwellZero

As far as I understand, this is because of the dot in post.id.
Name post_id for example.
Better yet, like this:
Guaranteed to pass only whole numbers.
Read more here:
flask.pocoo.org/docs/0.10/quickstart/#variable-rules

A
Alexey, 2015-03-07
@MAKAPOH

To generate links in flask there is a function url_for , it can be used both in code and in templates.
For such a route:

@app.route('/post/<int:post_id>')
def show_post(post_id):
    return 'Post %d' % post_id

The link can be obtained like this:
In order to do this for each post, we read them all from the database, run in a loop and substitute the id into the function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question