Error django MultiValueDictKeyError

0 votes

I'm trying to save an object to my database, but it's throwing a MultiValueDictKeyError error.

The line is

is_private = request.POST['is_private']

The problems lies within the form, the is_private is represented by a checkbox. If the check box is NOT selected, obviously nothing is passed. This is where the error gets chucked.

How do I properly deal with this exception, and catch it?

May 20, 2020 in Python by kartik
• 37,520 points
35,744 views

1 answer to this question.

0 votes

Hello @kartik,

Use the MultiValueDict's get method. This is also present on standard dicts and is a way to fetch a value while providing a default if it does not exist.

is_private = request.POST.get('is_private', False)

Generally,

my_var = dict.get(<key>, <default>)
answered May 20, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

Django Postgresql syncdb error

If you're trying to connect to a ...READ MORE

answered May 27, 2019 in Python by SDeb
• 13,300 points
1,554 views
+1 vote
1 answer

Django not found error

Before installing Django make sure you have ...READ MORE

answered Jun 24, 2019 in Python by Fata
• 1,050 points
2,618 views
0 votes
1 answer

Django: Display a custom error message for admin validation error

Hello @kartik, Without looking, it sounds like the ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,880 points
7,559 views
+1 vote
1 answer

Error:Getting Site Matching Query Does Not Exist Error after creating django admin

Hello @kartik, The Site object for your Django project is ...READ MORE

answered Jun 22, 2020 in Python by Niroj
• 82,880 points
12,491 views
0 votes
1 answer

how to download and install Django rest framework?

To install Django, you can simply open ...READ MORE

answered Apr 24, 2018 in Python by Christine
• 15,790 points
1,925 views
0 votes
1 answer
0 votes
1 answer

Host not allowed

Go to your project directory cd project cd project ALLOWED_HOSTS ...READ MORE

answered Aug 9, 2018 in AWS by Priyaj
• 58,100 points
1,699 views
0 votes
1 answer

Error: port is already in use of Django Server

Hii @kartik, Just type sudo fuser -k 8000/tcp. This ...READ MORE

answered May 6, 2020 in Python by Niroj
• 82,880 points
15,374 views
0 votes
1 answer

Error:django-debug-toolbar breaking on admin while getting sql stats

Hello @kartik, You just need to be able ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,880 points
1,228 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP