简体   繁体   中英

MultiValueDictKeyError when trying to get POST charfield value using django

i have a django code which post value and retrieve it inside views.py. but when i tried to retrieve the data it is throwing me MultiValueDictkeyError. I checked whether any multiple values are passed but no where multiple values are passed. Can anyone check on this.

Here is my html file.

 <form method="POST" enctype="multipart/form-data" >{% csrf_token %}
                     <p>  <label for="pa">App:</label>
    
                        <select name = "app">
                        <option value = "select" selected > Select   </option>
                        {% for i in result %}
                            <option value = "{{i.apptype}}" > {{i.apptype }}   </option>
                        {% endfor %}
                        </select>
                          &ensp; &ensp; &ensp; &ensp; &ensp; &ensp; &ensp; &ensp;&ensp; &ensp; &ensp; &ensp; &ensp; &ensp; &ensp; &ensp;
    
                        <label for="cu">Customer:</label>
    
                        <select name = "customerdrop">
                        <option value = "select" selected > Select   </option>
                        {% for j in result1 %}
                          <option value = "{{j.customer}}" > {{j.customer }}   </option>
                        {% endfor %}
                        </select>
    <button type="submit"style="background-color:#FFFF66;color:black;width:150px; height:40px;">Save Template</button>
                  <input type="button" style="background-color:#FFFF66;color:black;width:150px; height:25px;" value="Save Template as :"/>
                <input type="text" id="fname" name="fname" value=""/><br>
             </form>

Model.py

class config(models.Model):
    app = models.CharField(max_length=100)
    customerdrop = models.CharField(max_length=100)

views.py

def pg2(request):
    conn = pyodbc.connect('Driver={SQL Server};'
                          'Server=abc\abc;'
                          'Database=UI;'
                          'Trusted_Connection=yes;')

    if request.method == 'POST':
        print('inside Post')
        print(request.POST['app'])
        print(request.POST['customerdrop'])

so, it prints " inside Post ", and app too, but not customerdrop. Can anyone help me on this thing.

you need to handle the condition

try this

if request.method == 'POST':

    app = request.POST.get('app')


or 


if request.method == 'POST':
    
    try:

        app = request.POST['app']

    except:

        app = None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM