简体   繁体   English

尝试使用 django 获取 POST charfield 值时出现 MultiValueDictKeyError

[英]MultiValueDictKeyError when trying to get POST charfield value using django

i have a django code which post value and retrieve it inside views.py.我有一个 django 代码,它发布值并在views.py中检索它。 but when i tried to retrieve the data it is throwing me MultiValueDictkeyError.但是当我试图检索数据时,它抛出了 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.这是我的 html 文件。

 <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 Model.py

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

views.py视图.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.因此,它会打印“inside Post”和应用程序,但不会打印 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Django中获取发布值时出现MultiValueDictKeyError - MultiValueDictKeyError when getting a Post Value in Django 使用POST时Django中的MultiValueDictKeyError - MultiValueDictKeyError in Django when use POST Django queryset批注,具有选择项并尝试获取显示值的charfield - Django queryset annotation, charfield with choices and trying to get the display value 尝试发布图片时出现MultiValueDictKeyError - MultiValueDictKeyError when I trying to post image 尝试在 Django 中上传图像时出现 MultiValueDictKeyError - MultiValueDictKeyError when trying to upload image in Django 尝试检索“类型”时出现 django MultiValueDictKeyError - django MultiValueDictKeyError when trying to retrieve "type" 使用Django测试上传图像时出现MultiValueDictKeyError - MultiValueDictKeyError when upload an Image using Django Test Django:使用HttpResponseRedirect时/ app / logout /的MultiValueDictKeyError - Django : MultiValueDictKeyError at /app/logout/ when using HttpResponseRedirect django:如何从CharField和ModelChoiceField获得价值 - django: how to get value from CharField and ModelChoiceField 使用API​​RequestFactory测试GET端点时出现MultiValueDictKeyError - MultiValueDictKeyError when testing a GET endpoint using APIRequestFactory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM