我当时正在为我的大学做一个项目,它要求我为大学创建一个社交媒体类的网站,我想添加一个页面来显示所有用户,并在每个用户旁边提供一个按钮来关注他们。如果我们关注他们,他们名字旁边的按钮将显示为取消关注,反之亦然。
如何在html页面上显示关注和取消关注按钮?
代码
models.py
class Following(models.Model):
user=models.OneToOneField(User, on_delete=models.CASCADE)
users_ifollow=models.ManyToManyField(User, related_name='followed_by',blank=True, null=True)
people_followme=models.ManyToManyField(User, related_name='peeps',blank=True, null=True)
def __str__(self):
return f'{self.user.username} following'
def redirect_route(username):
return reverse('user-posts',args=[str(username)])
class Portfolio(models.Model):
user=models.OneToOneField(User, on_delete=models.CASCADE)
category=models.CharField(max_length=20, blank=True)
image1=models.ImageField(upload_to='profile_pics',blank=True)
image2=models.ImageField(upload_to='profile_pics',blank=True)
image3=models.ImageField(upload_to='profile_pics',blank=True)
image4=models.ImageField(upload_to='profile_pics',blank=True)
def __str__(self):
return f'{self.user.username} Portfolio'
views.py
def user_list(request):
authors=Portfolio.objects.all()
context={'author':authors}
return render(request, 'blog/userlist.html',context)
html
{% if user in u.followed_by %} <a class='mr-2' href='https://www.grayocean.co/user/{{u.user}}/follow'><button class='btn btn-danger'>Unfollow</button></a>
{% endif %}
<div class=''>