/savepost/ 处的 MultiValueDictKeyError

MultiValueDictKeyError at /savepost/

我知道之前有人问过这个问题,但我遇到了这个 MultiValueDictKeyError。基本上我试图从用户那里获取输入,输入标签有这个 name="usercaption" 属性。当我单击提交时,它会弹出 MultiValueDictKeyError。这是我的 HTML 表格:

<div class="post">
    <form action="/savepost/" method="GET">
        <input type="text" name="usercaption" placeholder="Write Something...">
        <div class="attach">
            <button class="upload-image"><i class="fal fa-image"></i> Image</button>
            <button><i class="fal fa-video"></i> Video</button>
            <button><i class="fal fa-smile-beam"></i> Mood</button>
            <button type="submit">Upload</button>
        </div>
    </form>
</div>

这是我的视图函数:

def savepost(request):
    caption = request.GET["usercaption"]
    Post = post(caption=caption)
    Post.save()
    return redirect('usersfeed')

错误在这一行caption = request.GET["usercaption"]

如果不存在,您必须提供默认值,例如:

 caption = request.GET.get["usercaption", ""]

您还必须添加用户:

user = request.user
Post = post(caption=caption, user=user)