List Python-flask logged-in user post

Asked

Viewed 260 times

1

I’m a beginner in flask and I’m doing an Appweb where the user can register and register and post, is a Virtual Arline system. I built the user with SESSION and can display only his data. In my database I have the users table, where are your data and the posts table. What I want to know now, is how I can list in the user profile, only the posts that he made, because the way I did so far, when I display the posts appear posts of all registered users. Here’s my code and thank you to help.

My table where I display the list of posts

<table class="table table-striped table-dark">
    <thead>
      <tr>
        <th scope="col">Tripulante</th>
        <th scope="col">Voo</th>
        <th scope="col">Origem</th>
        <th scope="col">Destino</th>
        <th scope="col">Duração</th>
        <th scope="col">Aeronave</th>
        <th scope="col">Data</th>
      </tr>
    </thead>
    <tbody>
      {% for post in post %}
      <tr>
        <th scope="row">{{ post.author.first_name }}</th>
        <td>{{ post.callsign_u}}</td>
        <td>{{ post.departure }}</td>
        <td>{{ post.arrival }}</td>
        <td>{{ post.flight_time }}</td>
        <td>{{ post.aircraft }}</td>
        <td>{{ post.last_seen.strftime('%d/%m/%Y') }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>

route that lists posts:

@app.route('/user/<username>')
@login_required
def user(username):
    va = db.session.query(func.sum(Post.flight_time)).first()    
    post = Post.query.order_by(Post.last_seen.desc()).all() #aqui que quero lista a postagem do usuario logado
    form1 = User.query.filter_by(username=username).first_or_404()    
    return render_template('user.html', form1=form1, post=post, va=va)

As I said, in this way I am selecting posts from all users and need help to list posts only from the currently logged in user.

1 answer

0

For this you need to filter the Post by users who created it.

In the flask_login the object of the logged-in user can be accessed through flask.ext.login.current_user:

from flask.ext.login import current_user

post = Post.query.filter(Post.user_id == current_user.id).order_by(Post.last_seen.desc()).all()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.