Glen Veigas
Glen Veigas

Reputation: 194

How to display the list of followers of a user in Flask

I am new to Flask, I have a created a social media blog using Flask and I have used flask-SQLAlchemy database. I have incorporated the follow feature in blog, now I want to display the list of followers of a user and I have been getting an error and the results are not getting displayed.

Here is the code: the route to display followers

@app.route("/userListFollowers/<username>")
@login_required
def listFollowers(username):    
    user = User.query.filter_by(username=username).first_or_404()
    folruser = user.followers.all()
    return render_template('userListFollowers.html', users=folruser)

the userListFollowers.html file that displays the list of followers

{%extends "layout.html" %}
{% block body %}
<div class="jumbotron">
  <h2 style="text-align: center;">Followers</h2>
  {% for users in listFollowers%}
        <a href="{{url_for('user', username=username.username)}}">{{username.username}}</a>
  {% endfor %}
</div>
{% endblock %}

I can't really understand how to get this working.

This is the errorError

Upvotes: 0

Views: 918

Answers (1)

Bennett Tomlin
Bennett Tomlin

Reputation: 137

You should be using render_template to render HTML templates, you are returning a redirect to the same view function so you keep endlessly redirecting.

Also you need to pass your list of followers into the render_template function so that it will render them with your Jinja code

For your jinja code:

{%extends "layout.html" %}
{% block body %}
<div class="jumbotron">
  <h2 style="text-align: center;">Followers</h2>
<ul>  
{% for users in listFollowers%}
        <li><a href="{{url_for('user', username=users.username)}}">{{users.username}}</a></li>
  {% endfor %}
</ul>
</div>
{% endblock %}

Upvotes: 1

Related Questions