Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Build a Social Network with Flask How to Win Friends Handling 404s

Yuan Gao
Yuan Gao
8,947 Points

where does the post id come from?

where does the post id come from?

1 Answer

Hi Yuan

post id comes from post model. Say i had a table with all posts and wanted to display them and also wanted the user to be able to look at each individual post. see below

# this is just an example
@app.route('/post/<int:myid>')
def post():
    post = models.Post.get(id=myid) # to get an individual post i need to  supply the route with an id
    return render('post.html',post=post)

home.html

 lets assume here i have supplied an array called posts with all the posts to home template

{% for post in posts%} # looping through each post
  <a href="{{url_for('post',myid=post.id)}}">post.title</a> # getting the id for each post and then navigate to post detail page 
{% endfor %}

post.html

<p>post.title</p>
<p>post.content</p>

hope this helps