JOEL LIAO
JOEL LIAO

Reputation: 11

MultiValueDictKeyError Request Method: GET (Django)

I have recently started to learn and work on Django i came across this error multiple times sometimes it was due to repetitive use of a name in an html and sometimes it was due the part for example "variable_name=request.POST['variable_name']" I visited various sites regarding this error and was able to find a solution i.e, once i have typed in "variable_name=request.POST['variable_name']" in a function in views.py

the next time i tried to POST the data again "variable_name=request.POST['variable_name']" I had to do something like this "variable_name=request.POST.get('variable-name', 'blank_or_whatever_canBeFalseAlso') The Above statement helped me solve that error I got this solution from A Thread in StackOverflow itself Quite a useful site.

Now for the Question am just Curious to know whats the difference between

  1. Variable_Name=request.POST['Variable_Name']

  2. Variable_Name=request.POST.get('Variable_Name','Whatever')

Code (views.py) Is given Below:

from django.shortcuts import render
from . import views
from datetime import date
from .models import Batch, Deleted_Batch
from django.utils.datastructures import MultiValueDictKeyError

# Create your views here.
today = date.today() 
Date_Batch = str(today.day)+'/'+str(today.month)+'/'+str(today.year)

def Reg_Batch(request):
    return render(request, 'Reg_Batch.html',{'Date_Batch':Date_Batch})

def Reg_Batch_Receive(request):
    Region_Code=request.POST['Region_Code']
    Branch_Code=request.POST['Branch_Code']
    Batch_Number=request.POST['Batch_Number']
    Farm_Code=request.POST['Farm_Code']
    Farm_Status=request.POST['Farm_Status']
    Farmer_Name=request.POST['Farmer_Name']
    Farmer_Address=request.POST['Farmer_Address']
    Farmer_Phone_Number=request.POST['Farmer_Phone_Number']
    Farmer_Email=request.POST['Farmer_Email']
    return render(request, 'Reg_Batch_Receive.html')

def Reg_Batch_View(request):
    Region_Code=request.POST.get('Region_Code', 'Blank')
    Branch_Code=request.POST.get('Branch_Code', 'Blank')
    Farm_Code=request.POST.get('Farm_Code', 'Blank')
    Farm_Status=request.POST.get('Farm_Status', 'Blank')
    return render(request, 'Reg_Batch_View.html',{'Region_Code':Region_Code,'Branch_Code':Branch_Code,'Farm_Code':Farm_Code,'Farm_Status':Farm_Status})

def Reg_Batch_View_Receive(request):
    Date_Batch= '20/08/2000'
    Region_Code='586001'
    Branch_Code='586001'
    Batch_Number='586001'
    Farm_Code='5484712'
    Farm_Status='Active'
    Farmer_Name='Joel Liao'
    Farmer_Address='jojojojojojojo'
    Farmer_Phone_Number='8088078086'
    Farmer_Email='[email protected]'
    return render(request,'Reg_Batch_View_Receive.html',{'Date_Batch':Date_Batch,'Region_Code':Region_Code,'Branch_Code':Branch_Code,'Batch_Number':Batch_Number,'Farm_Code':Farm_Code,'Farm_Status':Farm_Status,'Farmer_Name':Farmer_Name,'Farmer_Address':Farmer_Address,'Farmer_Phone_Number':Farmer_Phone_Number,'Farmer_Email':Farmer_Email})

def Reg_Batch_Delete(request):
    Region_Code='586001'
    Branch_Code='586001'
    Batch_Number='586001'
    Farm_Code='5484712'
    Farm_Status='Active'
    Farmer_Name='Joel Liao'
    Farmer_Address='jojojojojojojo'
    Farmer_Phone_Number='8088078086'
    Farmer_Email='[email protected]'
    Deleted_Batch_DataBase=Deleted_Batch({'Region_Code':Region_Code,'Branch_Code':Branch_Code,'Batch_Number':Batch_Number,'Farm_Code':Farm_Code,'Farm_Status':Farm_Status,'Farmer_Name':Farmer_Name,'Farmer_Address':Farmer_Address,'Farmer_Phone_Number':Farmer_Phone_Number,'Farmer_Email':Farmer_Email}),
    return render(request,'Reg_Batch_Delete.html',{'Batch_Number':Batch_Number})

def Reg_Batch_Delete_Receive(request):
    Batch_Number='586001'
    return render(request,'Reg_Batch_Delete_Receive.html',{'Batch_Number':Batch_Number})

Upvotes: 1

Views: 88

Answers (1)

wfehr
wfehr

Reputation: 2225

request.POST is a dictionary-like object (see docs).

So if you do

request.POST['foobar']

you can access the posted variable foobar. But if this is not being posted and therefore not set inside POST, you will get the MultiValueDictKeyError. This is similar to access a key inside a default dictionary which is not set, e.g.

d = {}
d['foobar']  # raises Exception

In order to avoid this error, you can then do

request.POST.get('foobar', 'default-value')

in order to get the posted field and a default if it is not present. So if foobar isn't set inside your POST-data, the received value is default-value.

This is similar to the default dictionary handling:

d = {}
d.get('foobar', 'random')  # returns 'random'

Upvotes: 2

Related Questions