0

I'm new to using Flask and Jinja for creating websites. I have these lines of code:

<h1 align="center">Feed</h1>
<ul class="list-group list-group-flush" id="feeds">
{% for my_feed in user.feeds %}
  <li class="list-group-item">
    <div>
      <b class="pull-center">{{user.first_name}}</b>
     <p class="pull-right">{{my_feed.date}}</p>
     <p>{{user.email}}</p>
    </div>
   <br>
    {{ my_feed.data }}

This though only displays me data pulled from currently logged-in users. What I'd like is to see on this page every post from every logged-in user.

I don't know how to do this. Do I need to change my Class (Feed) to make it MANY:MANY. relationships? Or does it suffice to change my little code above?

I want for everyone to be able to post but also read what everyone else posted. I have the code function like this:

@views.route('/tweets', methods = ['GET', 'POST'])
@login_required
def tweet():
    if request.method == 'GET':
        my_feed = MyFeed.query.all()
    elif request.method == 'POST':
        my_feed = request.form.get('my_feed')
        if len(my_feed) < 1:
            flash('Tweet is too short!', category='error')
        else:
            new_tweet = MyFeed(data=my_feed, user_id=current_user.id)
            db.session.add(new_tweet)
            db.session.commit()
            flash('Tweet added!', category='success')

    return render_template("tweets.html" ,user=current_user)
6
  • user is most probably referring to a single entity of all users. If you want to put it this way, it is only one row in a list of users. At some point you have to gather the data, probably using an ORM like SQL Alchemy to send all data to the frontend, not just a single user. Commented Nov 26, 2021 at 23:06
  • Could you please be a little more specific? How do I gather the data? I have all the information I need, stored in the tables I need, I need to just display them all without filtering them based on who’s logged in. I’m sorry, I’m new to this. Commented Nov 26, 2021 at 23:18
  • Maybe reed this: stackoverflow.com/questions/49964340/… Commented Nov 26, 2021 at 23:19
  • I'm not using the same data as he is, what I want is for everyone to be able to post but also read what everyone else posted. I have the code function like this: Commented Nov 27, 2021 at 13:56
  • @views.route('/tweets', methods = ['GET', 'POST']) @login_required def tweet(): if request.method == 'GET': my_feed = MyFeed.query.all() elif request.method == 'POST': my_feed = request.form.get('my_feed') if len(my_feed) < 1: flash('Tweet is too short!', category='error') else: new_tweet = MyFeed(data=my_feed, user_id=current_user.id) db.session.add(new_tweet) db.session.commit() flash('Tweet added!', category='success') return render_template("tweets.html" ,user=current_user) Commented Nov 27, 2021 at 13:57

0

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.