MultiValueDictKeyError on a Django form

huangapple go评论80阅读模式
英文:

MultiValueDictKeyError on a Django form

问题

以下是代码中的错误信息和相关部分的中文翻译:

MultiValueDictKeyError at /unfollow
多值字典关键错误

'userfollow'
'userfollow'

Request Method: POST
请求方法:POST
Exception Type: MultiValueDictKeyError
异常类型:MultiValueDictKeyError
Exception Value: 'userfollow'
异常值:'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文件中,你的HTML模板已经包括了一些中文字符,但没有明确的错误信息。

希望这可以帮助你找到代码中的问题。

英文:

I cant find the error in here when clicking the follow or unfollow button this message keeps coming.

MultiValueDictKeyError at /unfollow

'userfollow'

Request Method: POST <br>
Exception Type: MultiValueDictKeyError <br>
Exception Value:'userfollow'

I tried request.POST.get() and got another error.

views.py

def follow(request):
    userfollow = request.POST[&#39;userfollow&#39;]
    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={&#39;user_id&#39;: user_id}))

def unfollow(request):
    userfollow = request.POST[&#39;userfollow&#39;]
    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={&#39;user_id&#39;: user_id}))


profile.html

{% extends &quot;network/layout.html&quot; %}

{% block body %}
    &lt;h1&gt;{{ profile.username }}&lt;/h1&gt;

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

{% endblock %}

Help would be appreciated!

答案1

得分: 0

更正name&quot;userfollow&quot;name=&quot;userfollow&quot;,这样才能正确向后端发送参数。

英文:

There is a typo in

&lt;input type=&quot;hidden&quot; name&quot;userfollow&quot; value=&quot;{{ user_profile }}&quot; /&gt;

Change name&quot;userfollow&quot; to name=&quot;userfollow&quot;, this is not sending the parameter properly to the backend

huangapple
  • 本文由 发表于 2023年4月11日 00:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定