Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Build a Social Network with Flask How to Win Friends Follow and Unfollow Buttons

Error when displaying follow/unfollow button

I am getting errors when I try to display follower counts and follow/unfollow buttons.

If I remove the if/else statement then the counts display as follows: https://www.dropbox.com/s/napzcmrxzmwmwet/Screenshot.png?dl=0

Then, I add in the logic for the follow/unfollow buttons, i get this: https://www.dropbox.com/s/1goq80coikp8qg6/Screenshot1.png?dl=0

or this, if I add the ending parentheses on current_user.following: https://www.dropbox.com/s/o45ibp0txmjyzg4/Screenshot2.png?dl=0

My models.py file is identical to that given in the course download (I even tried copy and pasting to see if I was missing something that wasn't obvious).

user_stream.html

{% extends "stream.html" %}

{% block content %}
<div class="row">
    <div class="grid-25">
        <h1>{{ user.username }}</h1>
    </div>
    <div class="grid-50">
        <div class="grid-33">
            <h5>Posts</h5>
            <p>{{ user.posts.count() }}</p>
        </div>
        <div class="grid-33">
            <h5>Followers</h5>
            <p>{{ user.followers.count }}</p>
        </div>
        <div class="grid-33">
            <h5>Following</h5>
            <p>{{ user.following.count }}</p>
        </div>
    </div>
    <div class="grid-25">
        <!-- Follow/unfollow button -->
        {% if current_user.is_authenticated %}
            {% if user != current_user %}
                {% if not user in current_user.following %}
                    <a href="{{ url_for('follow', username=user.username) }}" class="small">Follow</a>
                {% else %}
                    <a href="{{ url_for('unfollow', username=user.username) }}" class="small">Unfollow</a>
                {% endif %}
            {% endif %}
        {% endif %}
    </div>
</div>

{{ super() }}
{% endblock %}

1 Answer

I got it working! Apparently, my followers and following methods in models.py needed @property decorators. Can anyone explain why that is? Kenneth Love ???

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That's a good question, especially on a course I'm no longer really familiar with. Let me do some searching and I'll get back to you.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Hmm, my only guess ATM is that something changed in Peewee. But based on what you have up there, would {{ user.following().count() }} work?