Django表单上的MultiValueDictKeyError

4zcjmb1e  于 2023-04-22  发布在  Go
关注(0)|答案(1)|浏览(76)

我找不到错误在这里,当点击跟随或取消跟随按钮,这条消息不断来。
MultiValueDictKeyError at /unfollow
用户关注
申请方式:POST
例外类型:MultiValueDictKeyError
异常值:“userfollow”
我尝试了request.POST.get(),得到了另一个错误。
views.py

def follow(request):
    userfollow = request.POST['userfollow']
    currentUser = User.objects.get(pk=request.user.id)
    userfollowData = User.objects.get(username=userfollow)
    f = Follow(user=currentUser, user_follower=userfollowData)
    f.save()
    user_id = userfollowData.id 
    return HttpResponseRedirect(reverse(profile, kwargs={'user_id': user_id}))

def unfollow(request):
    userfollow = request.POST['userfollow']
    currentUser = User.objects.get(pk=request.user.id)
    userfollowData = User.objects.get(username=userfollow)
    f = Follow.objects.get(user=currentUser, user_follower=userfollowData)
    f.delete()
    user_id = userfollowData.id 
    return HttpResponseRedirect(reverse(profile, kwargs={'user_id': user_id}))

profile.html

{% extends "network/layout.html" %}

{% block body %}
    <h1>{{ profile.username }}</h1>

    <div class="container">
        <div class="row d-flex justify-content-center">
            <h3 class="col-4">Followers: {{ followers.count}} </h3>
            <h3 class="col-4">Following: {{ following.count}} </h3>
            {% if user.is_authenticated %}
                {% if user != user_profile %}
                    {% if isFollowing %}
                        <form action="{% url 'unfollow' %}" method="post">
                            {% csrf_token %}
                            <input type="hidden" name"userfollow" value="{{ user_profile }}" />
                            <input type="submit" value="Unfollow" />
                        </form>
                    {% else %}
                        <form action="{% url 'follow' %}" method="post">
                            {% csrf_token %}
                            <input type="hidden" name"userfollow" value="{{ user_profile }}" />
                            <input type="submit" value="Follow" />
                        </form>
                    {% endif %}
                {% endif %}
            {% endif %}
        </div>
    </div>

{% endblock %}

帮助将不胜感激!

vxbzzdmp

vxbzzdmp1#

中有一个打印错误

<input type="hidden" name"userfollow" value="{{ user_profile }}" />

name"userfollow"更改为name="userfollow",这无法将参数正确发送到后端

相关问题