[英]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>
                              
<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.