Подписаться/отписаться от автора поста в django
нужна помощь, создаю систему подписки/отписки от пользователей блога по аналогии с Твитером или Инстаграмм. То есть любой зарегистрированный пользователь на сайте может подписываться/отписаться от любого пользователя. При нажатии на кнопку отписаться/подписаться выдает ошибку HTTP ERROR 405. Вот код модели:
class Author(AbstractUser):
avatar = models.ImageField(null=True, blank=True, upload_to="images/profile/")
bio = models.TextField(null=True, blank=True)
date_of_birth = models.DateField(null=True, blank=True)
facebook_url = models.CharField(max_length=255, null=True, blank=True)
twitter_url = models.CharField(max_length=255, null=True, blank=True)
instagram_url = models.CharField(max_length=255, null=True, blank=True)
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 Author.objects.filter(follows=self).count()
Код вьюшки:
class AuthorDetailView(DetailView):
model = Author
template_name = "registration/author_detail.html"
def follow_user(request, pk):
if request.user.is_authenticated:
profile = Author.objects.get(user_id=pk)
if request.method == "POST":
current_user_profile = request.user.profile
action = request.POST["follow"]
if action == "unfollow":
current_user_profile.follows.remove(profile)
elif action == "follow":
current_user_profile.follows.add(profile)
current_user_profile.save()
return render(request, "registration/author_detail.html", {"profile": profile})
код html странички:
<form method=POST>
{% csrf_token %}
{% if author in author.follows.all %}
<button class="btn btn-outline-danger" name="follow"
value="unfollow" type="submit">Unfollow @{{ author.username | lower }}
</button>
{% else %}
<button class="btn btn-outline-success" name="follow"
value="follow" type="submit"> Follow @{{ author.username | lower }}
</button>
{% endif %}
</form>
Не могу понять, где ошибка?