Hello, new to django and here…
I’m finding it difficult to implement a follower – following system with an Abstractuser model.
I can update followers for each user profile in the backend which is reflected and shows the right button (follow / unfollow) on the users profile — so the logic is working.
However when I try to create my views.py file so that the user can follow / unfollow themselves nothing I’ve tried works (granted I know nothing) but if anyone can point me in the right direction would be greatly appreciated.
Here are snippets of my code;
# models.py
class NewUser(AbstractUser):
birthdate = models.DateField(max_length=8, null=True, blank=True)
portrait = models.ImageField(upload_to='portrait', default="../static/portrait/portrait_placeholder.png", null=True, blank=True)
bio = models.TextField(verbose_name='Biography', max_length=600, null=True, blank=True)
slug = AutoSlugField(populate_from='first_name', unique_with='last_name')
follows = models.ManyToManyField("self", related_name="followed_by", symmetrical=False, blank=True)
def count_followers(self):
return self.follows.count()
def count_following(self):
return NewUser.objects.filter(follows=self).count()
def __str__(self):
return self.username
class Product(models.Model):
creator = models.ForeignKey(NewUser, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField("Product Name", max_length=100, default="", help_text="This is the help text")
concept = models.TextField(verbose_name='Concept', max_length=600, blank=True)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
likes = models.ManyToManyField(NewUser, related_name='product_likes', blank=True)
def total_likes (self):
return self.likes.count()
class Meta:
ordering = ['-created_date']
# views.py
# view of populated profile detail view with associated profile products
class ProfileDetailView(DetailView):
model = NewUser
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the users products
context['object_list'] = Product.objects.filter(creator=self.object)
return context
def follow_user(request, pk):
follow_user = NewUser.objects.get(pk=request.user.id)
if request.method == "POST":
current_user_profile = request.user
action = request.POST['follow']
if action == "unfollow":
current_user_profile.follows.remove(follow_user)
elif action == "follow":
current_user_profile.follows.add(follow_user)
return render(request, 'authenticate/profile_detail.html', {"follow_user":follow_user})
# newuser_detail.html
<div class="row">
<div class="col-md-auto p-3">
<div class="button mt d-flex flex-row align-items-center">
<a href="" class="h6">
Following {{ newuser.count_followers }}
<br>
{% for username in newuser.follows.all %}
<a href="{% url 'profile' username.slug %}"> @{{ username }} </a><br/>
{% endfor %}
</div>
</div>
<div class="col-md-auto p-3">
<div class="button mt d-flex flex-row align-items-center">
<a href="" class="h6">
Followers {{ newuser.count_following }}
<br>
{% for username in newuser.followed_by.all %}
<a href="{% url 'profile' username.slug %}"> @{{ username }} </a><br/>
{% endfor %}
</div>
</div>
</div>
<form method=POST>
{% csrf_token %}
{% if newuser in user.follows.all %}
<button class = "btn btn-outline-danger" name="follow" value="unfollow" type="submit">unfollow {{ newuser.first_name }}</button>
{% else %}
<button class = "btn btn-outline-success" name="follow" value="follow" type="submit">follow {{ newuser.first_name }}</button>
{% endif %}
</form>