I\'m trying to save a object to my database, but it\'s throwing a MultiValueDictKeyError
error.
The problems lies within the form, the is_private<
For me, this error occurred in my django project because of the following:
I inserted a new hyperlink in my home.html present in templates folder of my project as below:
In views.py, I had the following definitions of count and about:
def count(request):
fulltext = request.GET['fulltext']
wordlist = fulltext.split()
worddict = {}
for word in wordlist:
if word in worddict:
worddict[word] += 1
else:
worddict[word] = 1
worddict = sorted(worddict.items(), key = operator.itemgetter(1),reverse=True)
return render(request,'count.html', 'fulltext':fulltext,'count':len(wordlist),'worddict'::worddict})
def about(request):
return render(request,"about.html")
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.homepage,name="home"),
path('eggs',views.eggs),
path('count/',views.count,name="count"),
path('about/',views.count,name="about"),
]
As can be seen in no. 3 above,in the last url pattern, I was incorrectly calling views.count whereas I needed to call views.about.
This line fulltext = request.GET['fulltext']
in count function (which was mistakenly called because of wrong entry in urlpatterns) of views.py threw the multivaluedictkeyerror exception.
Then I changed the last url pattern in urls.py to the correct one i.e. path('about/',views.about,name="about")
, and everything worked fine.
Apparently, in general a newbie programmer in django can make the mistake I made of wrongly calling another view function for a url, which might be expecting different set of parameters or passing different set of objects in its render call, rather than the intended behavior.
Hope this helps some newbie programmer to django.