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 in Python by kartik
• 27,140 points
1,268 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 by Niroj
• 58,500 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,250 points
270 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
342 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 in Python by Niroj
• 58,500 points
568 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 in Python by Niroj
• 58,500 points
715 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,810 points
435 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
• 57,640 points
582 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 in Python by Niroj
• 58,500 points
458 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 in Python by Niroj
• 58,500 points
120 views