POST视图上不允许使用Django错误http方法

时间:2017-08-01 07:57:46

标签: django django-views

我有一个错误说明方法不允许(POST)。但我认为我有一个Post方法。这是我认为的唯一方法。

class AddOrRemoveFollower(LoginRequiredMixin, View):

    def post(self, request, *args, **kwargs):
        #import ipdb; ipdb.set_trace()
        other_user = get_object_or_404(User, pk=kwargs['pk'] )
        current_user = request.user
        if current_user is other_user:
            messages.success(request, 'You cannot follow yourself')
            return redirect(reverse('myfriendship:friends'))
        if current_user not in Follow.objects.followers(other_user):
            Follow.objects.add_follower(request.user, other_user)
            messages.success(request, 'You are now following a user')
            return redirect(reverse('myfriendship:friends'))
        else:
            Follow.objects.remove_follower(request.user, other_user)
            messages.success(request, 'You decided to unfollow this user')
            return redirect(reverse('myfriendship:friends'))

urls.py

url(r'^AddOrRemoveFollower/(?P<pk>\d+)/$', 
    views.AddOrRemoveFollower.as_view(), 
    name='AddOrRemoveFollower'),

WARNING:django.request:Method Not Allowed (POST): /myfriendship/AddOrRemoveFollower/1/
WARNING:django.server:"POST /myfriendship/AddOrRemoveFollower/1/ HTTP/1.1" 405 0

HTML

<form class="right" method="POST" action="{% url 'myfriendship:AddOrRemoveFollower' user.id %}">
 {% csrf_token %}
   <input type="hidden" name="course_id" value="{{user.id}}">
   <input class="btn btn-primary btn-sm red darken-2" type="submit" value="{% can_follow user request.user %}">
</form> 

1 个答案:

答案 0 :(得分:-2)

您尝试发布详细信息,只允许没有pk的类基本视图中的POST方法。尝试PUT或PATCH。