我有一个django代码,它发布值并在views.py中检索它。但是当我试图检索数据时,它抛出了MultiValueDictkeyError。我检查了是否传递了多个值,但没有传递多个值。有没有人能检查一下。
这是我的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
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'])所以,它会打印“inside Post”,还有app,但不会打印customerdrop。有人能帮我解决这件事吗。
发布于 2021-03-03 21:27:24
你需要处理这种情况
尝尝这个
if request.method == 'POST':
app = request.POST.get('app')
or
if request.method == 'POST':
try:
app = request.POST['app']
except:
app = Nonehttps://stackoverflow.com/questions/66457868
复制相似问题