0

I know this has been asked before but I'm facing this MultiValueDictKeyError. Basically I'm trying to take an input from the user, the input tag has this name="usercaption" attribute. While I click on submit it pops up the MultiValueDictKeyError. Here's my HTML form:

<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>

Here's my view function:

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

The error is on this line caption = request.GET["usercaption"]

notVansh
  • 207
  • 4
  • 15
  • Does this answer your question? [django MultiValueDictKeyError error, how do I deal with it](https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) – mahalde Aug 06 '20 at 10:39

1 Answers1

0

You have to provide a default value for if it doesn't exist, so for example:

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

And you have to add the user as well:

user = request.user
Post = post(caption=caption, user=user)
MeL
  • 1,269
  • 3
  • 12
  • 30