我不明白为什么我的request.POST返回空数据。这是我从控制台获得的字符串:
<QueryDict: {u'csrfmiddlewaretoken': [u'qPBJWRB3zUubmGYQ4U414asnnNYb22C3'], u'taxiList': [u''], u'deleteTaxi': [u'']}>
taxiList是包含一些条目的下拉列表。
视图
def admindashboard(request):
jobsNow = Job.objects.filter(jobstatus = 'd')
jobsAvailable = Job.objects.filter(jobstatus='p')
taxis=Taxi.objects.all()
template = "admindashboard.html"
form = AdminDashboardForm()
if request.method == 'GET':
form = AdminDashboardForm()
else:
if request.POST.get("cancelJob"):
selectedJob = request.POST.get("liveJobs")
selectedJobObj = Job.objects.get(pk = selectedJob)
selectedJobObj.jobstatus = 'p'
selectedJobObj.taxi = None
selectedJobObj.save()
elif "deleteTaxi" in request.POST.get():
test1 = request.POST
print(test1)
selectedTaxi = request.POST.get("taxiList")
selectedTaxiObj = Taxi.objects.get(LicensePlate='MFR011')
selectedTaxiObj.delete()
elif request.POST.get("deleteJob"):
selectedAvailableJob = request.POST.get("availableJobs")
selectedAvailableJobObj = Job.objects.get(pk= selectedAvailableJob)
elif request.POST.get("assignJob"):
selectedAvailableJob = request.POST.get("")
else:
test1 = request.POST
print(test1)
print("no action was provided")
return render(request, template, {'jobsNow':jobsNow, 'form':form, 'taxis':taxis, 'jobsAvailable':jobsAvailable })
HTML
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Taxis</h3>
</div>
<div class="panel-body">
<p>
<select name="taxiList" class="form-control">
{% for taxis in taxis %}
<option value="{{taxis.LicensePLate}}">{{taxis.LicensePlate}}</option>
{% endfor %}
</select></p>
<button name ="addTaxi" onclick="addTaxiFct()" type="button" class="btn btn-sm btn-success">Ajouter Taxi</button>
<button name="deleteTaxi" type="submit" class="btn btn-sm btn-danger">Éffacer Taxi</button>
<button type="button" class="btn btn-sm btn-primary">Modifier</button> </p>
</div>
</div>
</div>
我的理解是request.POST
应该发回一个包含dropdown
中所选元素的字符串。我正在调试,这就是为什么我的模型对deleteTaxi
元素有不同的条件。