Having Problems Displaying followed users posts in Python Flask

So I have a problem with my project. I am trying to display the user's followings posts but nothing is displayed. I don't know what the problem is as no errors are being displayed. This is what I am using to help me to query followed posts (page 158)

This is where the posts should be displayed. This gives the user the option to show all posts or just followed posts

Nothing is being displayed though.

Here is the User class where I am defining followed posts:

The function followed_post() is supposed to display the users followed posts

 @property
def followed_posts(self):
    return Post.query.join(Follow, Follow.followed_id == Post.user_id).filter(Follow.follower_id == self.id)

In my main routes i have ;

@main.route('/all')
@login_required
def show_all():
    resp = make_response(redirect(url_for('main.compscipost')))
    resp.set_cookie('showed_followed', '' , max_age = 
    30*24*60*60)
return resp

@main.route('/followed')
@login_required
def show_followed():
   resp = make_response(redirect(url_for('main.HomePage')))
   resp.set_cookie('showed_followed', '1', max_age = 30*24*60*60)
return resp

and also in the blueprint =' main' routes.py, the function for my homepage is :

@main.route('/')
@main.route('/home')
def HomePage():
  page = request.args.get('page', 1, type = int)
  showed_followed = False
  if current_user.is_authenticated:
    showed_followed = bool(request.cookies.get('showed_followed', ''))
  if showed_followed:
     query= current_user.followed_posts
  else: 
    query = Post.query
  pagination = 
query.order_by(Post.date_posted.desc()).paginate(page = page, 
per_page = 5, 
        error_out = False)
posts = pagination.items
return render_template("HomePage.html", posts =posts, 
showed_followed = showed_followed , 
    pagination = pagination)

Finally for my homepage.html which I think is where the main problem is :

this is the homepage.html .

This is a pastebin of the html

1 answer

  • answered 2018-10-11 20:08 Charming Robot

    That's because you're just building a query in your def followed_posts() property.

    You should end it with .one() or .all() or first().

    On a side note, if you're using SQLAlchemy take a look at hybrid properties. As regular Python properties will sometimes cause you problems when you're querying with instance and class attributes at the same time.