我正在尝试构建一个用户可以放置地址的简单表单。提交表单后,我收到错误消息:
MultiValueDictKeyError at /store/order/
"'street'"
我认为街道地址的数据没有正确提交,这很奇怪,因为我知道street
输入不是空的。当我尝试用street = request.POST['street']
替换street = request.POST.get('street', '')
时,所有内容都标记为空,因此我知道数据未发布。这种情况最初是定期发生的,但现在它是不变的。有什么想法吗?
views.py
if request.method == "POST":
street = request.POST['street']
street_num = street.split(" ", 1)[0]
city = request.POST['city']
state = request.POST['state']
zip_code = request.POST['zip']
street_name = street.split(" ", 1)[1]
order.shipping = str(street_num) + " " + str(street_name) + ", " + str(city) + " " + str(zip_code) + ", " + str(state) + ", " + 'US'
form.html
<form enctype='multipart/form-data' action='/order/' method="POST" id="shipping-form">{% csrf_token %}
<span class="lead">Shipping Address</span>
<div class="col-md-12 form-group">
<label class="upper text-dark">
<span>Street Address</span>
<input id="street" name="street" type="text" size="50" placeholder="'123 Artist St'">
</label>
</div>
<div class="col-md-6 form-group">
<label class="upper text-dark">
<span>City</span>
<input id="city" name="city" type="text" size="50" placeholder="'San Francisco'">
</label>
</div>
<div class="col-md-3 form-group">
<label class="upper text-dark">
<span>State</span>
<input id="state"name="state" type="text" size="2" placeholder="'CA'">
</label>
</div>
<div class="col-md-3 form-group">
<label class="upper text-dark">
<span>Zip Code</span>
<input id="zip" name="zip" type="text" size="4" placeholder="'12345'">
</label>
</div>
</form>
我知道我没有<input type="submit">
标签。我有多个表单,因此提交正在javascript中处理。但是,我的其他形式是有效的,它们看起来完全相同,所以我很困惑为什么这个形式不能工作
编辑:Javascript(我使用Stripe进行付款,因此会在提交Credit表单后立即执行)
document.getElementById("shipping-form").submit();