【发布时间】:2018-11-11 13:45:15
【问题描述】:
我已经搜索并实现了许多不同的方法来显示一个 html 表,使用 AJAX,来自 JsonResponse (Django) - 但无济于事。
目前,我得到的最远的是对网络控制台的响应:
{"products": "[{\"model\": \"products.product\", \"pk\": 2, \"fields\": {\"name\": \"Bag\", \"description\": \"Carries items conspicuously \", \"price\": \"10.99\"}}, {\"model\": \"products.product\", \"pk\": 3, \"fields\": {\"name\": \"iPhone 8 Plus\", \"description\": \"a mobile device from Apple\", \"price\": \"850.00\"}}, {\"model\": \"products.product\", \"pk\": 8, \"fields\": {\"name\": \"Shoes\", \"description\": \"For your feet\", \"price\": \"49.50\"}}, {\"model\": \"products.product\", \"pk\": 9, \"fields\": {\"name\": \"Gloves\", \"description\": \"For your hands\", \"price\": \"2.99\"}}, {\"model\": \"products.product\", \"pk\": 10, \"fields\": {\"name\": \"Blanket\", \"description\": \"Keep warm\", \"price\": \"13.79\"}}, {\"model\": \"products.product\", \"pk\": 11, \"fields\": {\"name\": \"Gown\", \"description\": \"Sleep time\", \"price\": \"25.99\"}}]"}
但我希望这本词典通过 ajax 显示在我的 html 表中
我的 django 模型如下所示:
class Product(models.Model):
def __str__(self):
return self.name
name = models.CharField(max_length = 200)
description = models.TextField()
price = models.DecimalField(decimal_places=2,max_digits=100000)
我的 Django 视图:
def product_list(request):
productData = serializers.serialize("json", Product.objects.all())
return JsonResponse({"products": productData})
我的html页面正文:
<body style='text-align:center'>
<table class="table table-striped" id="product-table">
<thead class="thead-dark"><th>Item<th>Description<th>Price</th><th></th></thead>
<tr>
<form id="add-product">{% csrf_token %}
<td><a><input type="text" class="form-control form-control-sm" id="newProductName" placeholder="Product name"></a></td>
<td><a><input type="text" class="form-control form-control-sm" id="newProductDesc" placeholder="Product description"></a></td>
<td><a><input type="text" class="form-control form-control-sm" id="newProductPrice" placeholder="£--.--"></a></td>
<td><button type="submit" class="btn btn-primary btn-sm" id="newProductSubmit">add</button></td>
</form>
</tr>
</table>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="{% static 'main.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
和我的 ajax 函数来获取 jsonresponse:
$(document).ready(function() {
$.ajax({
url: 'products/getProducts/',
datatype: 'json',
type: 'GET',
success: function (data) {
alert("Got data!")
jData = JSON.parse(data)
alert("got json products!");
}
});
});
使用 ajax,我知道我缺少将数据转换为 html 并附加到我的表格的东西,但是当我尝试这个时,它根本没有添加任何东西
【问题讨论】:
-
不完全清楚您想在表单输入字段或表格中显示响应数据?
-
@zollie 在一个表格中,表格是一个单独的部分
-
好的,我正在检查并回复您。
-
您已经对数据进行了双重编码。您不应该将序列化的 JSON 结果放入另一个本身由 JsonResponse 序列化的 dict。