1

I sufering from the problem with my code, whenever i entered my data into the database and then at time when i click submit it will through an error Multyvaluekeyerror. i change my form value so many times but nothing is working. Please help me out this.....it very be thankfull.


    MultiValueDictKeyError at /admin/criminals
    'gender'
    Request Method: POST
    Request URL:    http://127.0.0.1:8000/admin/criminals
    Django Version: 2.2.4
    Exception Type: MultiValueDictKeyError
    Exception Value:    
    'gender'
    Exception Location: C:\Users\lenovo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\datastructures.py in __getitem__, line 80
    Python Executable:  C:\Users\lenovo\AppData\Local\Programs\Python\Python37-32\python.exe
    Python Version: 3.7.4
    Python Path:    
    ['D:\\django project\\gadmin3',
     'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
     'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
     'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
     'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32',
     'C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python37\\site-packages',
     'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
    Server time:    Mon, 7 Oct 2019 04:35:54 +0000

MAIN FORM CODE:-
    <div class="form-group">
       <label class="control-label col-md-3 col-sm-3 col-xs-12">Gender</label>
       <div class="col-md-6 col-sm-6 col-xs-12">
          <div name="gender" id="" class="btn-group" data-toggle="buttons">
             <label class="btn btn-default" data-toggle-class="btn-primary" 
                data-toggle-passive-class="btn-default">
             <input type="radio" name="" value="male" data-parsley- 
                multiple="gender"> &nbsp; Male &nbsp;
             </label>
             <label class="btn btn-primary" data-toggle-class="btn-primary" 
                data-toggle-passive-class="btn-default">
             <input type="radio" name="" value="female" data-parsley- 
                multiple="gender"> Female
             </label>
          </div>
       </div>
    </div>

VIEW SIDE CODE:-


    def criminals(request):
        if request.method=="POST":
            cn = request.POST['crname']
            ccrime = request.POST['crime']
            cage = request.POST['age']
            cheight=request.POST['height']
            cbody = request.POST['bodymark']
            crgen = request.POST['gender']
            s= Criminals()
            s.mname=cn
            s.mcrime=ccrime
            s.mage=cage
            s.image = request.FILES['photo']
            s.mheight=cheight
            s.mbody=cbody
            s.mgender=crgen
            s.save()
            messages.success(request,"Criminal Added Successfully.")
            return render(request,'criminal.html')
        else:
            return render(request,'criminal.html')

Encaledus
  • 51
  • 9

2 Answers2

1

gender is a radio type button (also add the name in your form) and in case of not selected any option then you will not get any input.

To avoid this issue use the get method for extracting the value from POST request.

crgen = request.POST.get('gender', default_value)

This will eliminate the MultiValueDictKeyError issue

Saleem Ali
  • 1,363
  • 11
  • 21
1

Your inputs have name in them e.g

<input type="radio" name="" value="female" data-parsley- 

This should be

<input type="radio" name="gender" value="female" data-parsley- 

Your code can be futher refactored to:

def criminals(request):
             if request.method=="POST":
                    s= Criminals(mname=request.POST['crname'],
                                ccrime=request.POST['crime'], 
                                mage=request.POST['age'],
                                mheight= request.POST['height'],
                                mbody=request.POST['bodymark'], 
                                mgender=request.POST['gender'])
                    s.save()
                    messages.success(request,"Criminal Added Successfully.")
                    return render(request,'criminal.html')
                else:
                    return render(request,'criminal.html')
phang
  • 518
  • 4
  • 20