This is my User model
class User(AbstractUser):
authors = models.ManyToManyField(
"self", blank=True, related_name="fans", symmetrical=False
)
def __str__(self):
return str(self.username)
This is my User model
class User(AbstractUser):
authors = models.ManyToManyField(
"self", blank=True, related_name="fans", symmetrical=False
)
def __str__(self):
return str(self.username)
This is another condition that I think you have reversed. You’re checking here if the user
(person looking at the page) is being followed by the person the page is about (profile
)
I think you’re wanting to check to see if the user
viewing this page is already a fan of that author.
Can you provide more details or be more specific about what you mean by this? What are you looking at to establish this? Have you looked at the database itself (either directly or using the Django admin) to see whether what you’re seeing is accurate?
Like this
{% if user in profile.fans.all %}
Since Profile 1 is following Profile 2, the above should be
Test 1 and Test 2
But in the below screenshot, its showing Test 1 and Test therefore the author and fan are Test 1/Profile 1
Also, when I click the FOLLOW button on Profile 2/Test 2 I should see the UNFOLLOW button on Profile 2/Test 2, but when redirected to Profile 1, the unfollow button shows on Test/Profile 1 and then Test/Profile 1 is both the follower/author and the following_user/fan.
I’ve checked the admin section, Test 1 is the author of itself
Since spaces and newlines can be ignored, there’s no direct way to determine which loop is producing that data.
With the various tests, etc, being run, are you sure that the many-to-many join table is empty before you start?
Do you mean checking to see that the author is not following anyone before I click on follow at the initial start of runserver?
Is this plausible?
More accurately that no one is following anyone when you start.
Not particularly. The issue is likely that you have some leftover data in that join table that needs to be cleaned out.
Yes, within my plenty trials and error, I’ve also deleted my database and cleared my cache.
I am using sqlite and have previously deleted the user admin and deleted my database so I don’t know why it’s happening.
I would clear the cache and try again perhaps.
Thank you
That is good to know.
I would suggest that when you see some strangeness like this happening, you use the sqllite3 command-line tool to examine the database and look at the many-to-many table created for this relationship to see if there’s anything odd in that table.
I haven’t used it before but I’d research how to do this
I just want to say thank you so much for taking the time to assist me with my code.
I’ve been able to resolve the error and get my follow/unfollow functionality after literally weeks of struggling with what would seem basic to some people THANKFULLY with your help.
MODELS.PY
class User(AbstractUser):
follows = models.ManyToManyField(
"self", blank=True, related_name="fans", symmetrical=False
)
def __str__(self):
return str(self.username)
VIEWS.PY
def profile(request, user_id):
profile = User.objects.get(pk=user_id)
post_count = NewTweet.objects.filter(user = user_id).count()
profile_post = NewTweet.objects.filter(user = user_id)
if request.method == "POST":
current_user_profile = request.user
if user_id != request.user.id:
if request.user.follows.filter(pk=user_id).exists():
current_user_profile.follows.remove(profile)
is_following = False
else:
current_user_profile.follows.add(profile)
is_following = True
context = {
"profile": profile,
"post_count": post_count,
"is_following": is_following,
"profile_post": profile_post,
}
current_user_profile.save()
return render(request, "network/profile.html", context)
context = {
"profile": profile,
"post_count": post_count,
"profile_post": profile_post,
}
return render(request, "network/profile.html", context)
PROFILE.HTML
<form method="POST">
{% csrf_token %}
{% if user.is_authenticated and profile != request.user %}
{% if is_following %}
<button class="btn btn-primary rounded-pill my-1 mx-1 py-1 py-1" type="submit">
Unfollow
</button>
{% else %}
<button class="btn btn-primary rounded-pill my-1 mx-1 py-1 py-1" type="submit">
Follow
</button>
{% endif %}
{% else %}
<a href="{% url 'logout' %}" class="btn btn-primary rounded-pill my-1 mx-1 py-1 py-1">Logout</a>
{% endif %}
</form>
<ul class="about">
{% if profile.follows.count <= 1 %}
<li><span>{{ profile.follows.count }}</span> Follower</li>
{% else %}
<li><span>{{ profile.follows.count }}</span> Followers</li>
{% endif %}
<li><span>{{ profile.fans.count }} </span>Following</li>
{% if post_count <= 1 %}
<li><span>{{post_count}} </span>Post</li>
{% elif post_count >= 2 %}
<li><span>{{post_count}} </span>Posts</li>
{% endif %}
</ul>
Just in case you’re not aware of how much of a big role your being here and answering questions affects live, I hope you see this extra thank you and know that on the other end, you are changing lives - thank you.
Thank you, that’s very kind of you to say. And if you run into any further problems, we’ll still be here.