Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Flask]filter posts from list
#1
I need to filter through posts from a list of user names. I can't figure it out tough so I need some help. The other solution is removing certain posts from a paginated list of posts that are unfiltered. The filtering looks something like this:
Post.query.filter_by(author=UsernameListToFilterBy).all() #I need to filter through a list
Thanks in advance

def home():
    page = request.args.get('page', 1, type=int)
    postFiltered = []
    for followed in current_user.followingList:
        postFiltered.append(Post.query.filter_by(author=followed).first()) #Here I created a list of posts by followed users
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5) #Here I need to filter from the list or...
    for post in posts.items:
        if User.query.filter_by(username=post.author.username) in current_user.followingList:
            #Code
        else:
            #remove post from posts

    return render_template('Home.html', posts=posts)

I just made a remove item function in the pagination class and removed a post if it was not being followed by the current user
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range" & "TypeError: The view function f: Flask Web App joelbeater992 5 3,454 Aug-31-2021, 08:08 PM
Last Post: joelbeater992
  [Flask] How to paginate a list of posts SheeppOSU 2 4,325 Jun-22-2019, 07:45 PM
Last Post: SheeppOSU
  problem using drop down list to filter table. darktitan 1 2,761 Mar-27-2019, 09:57 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020