Jquery 在AJAX调用之后,如何更新django模板上下文变量?
我有一个表产品,它显示了一组产品的信息Jquery 在AJAX调用之后,如何更新django模板上下文变量?,jquery,ajax,django,django-templates,Jquery,Ajax,Django,Django Templates,我有一个表产品,它显示了一组产品的信息 <table id="item_table" class="table table-sm table-hover table-bordered"> <thead class="thead-inverse"> <tr> <th colspan="2">Date</th> <th colspan="6">
<table id="item_table" class="table table-sm table-hover table-bordered">
<thead class="thead-inverse">
<tr>
<th colspan="2">Date</th>
<th colspan="6">Product name</th>
<th colspan="2">Category</th>
<th colspan="2">Amount</th>
</tr>
</thead>
<tbody>
{% for item in product_list %}
<tr>
<td colspan="2">{{ item.date }}</td>
<td id="item_name_format" colspan="6">{{ item.name }}</td>
{% if item.category_id %}
<td id="item_name_format" colspan="2">{{ item.category_id.level1_desc }}</td>
{% endif %}
<td id="item_amt_format" colspan="2">${{ item.amount|intcomma }}</td>
</tr>
{% endfor %}
</tbody>
</table>
如何使用Ajax调用的“结果”更新变量product_列表
这应该会更新表,对吗
谢谢你不能这样。更好的方法是通过ajax加载html的这一部分 您的ajax视图:
def update_items(request):
product_list = your_data
return render(request, 'table_body.html', {'product_list':product_list})
您的主要html:
<tbody class="table_body">
{% include 'table_body.html' %}
</tbody>
{%include'表_body.html%}
表_body.html:
{% for item in product_list %}
<tr>
<td colspan="2">{{ item.date }}</td>
<td id="item_name_format" colspan="6">{{ item.name }}</td>
{% if item.category_id %}
<td id="item_name_format" colspan="2">{{ item.category_id.level1_desc }}</td>
{% endif %}
<td id="item_amt_format" colspan="2">${{ item.amount|intcomma }}</td>
</tr>
{% endfor %}
{%用于产品列表%中的项目]
{{item.date}
{{item.name}
{%if item.category_id%}
{{item.category\u id.level1\u desc}
{%endif%}
${{item.amount | intcomma}}
{%endfor%}
您的ajax将如下所示:
function update_item(item_num) {
console.log(item_num) // sanity check
$('.table_body').html('').load(
"{% url 'update_items' %}?item_num=" + item_num
); // <--- this code instead of $.ajax(lala)
功能更新项目(项目编号){
console.log(item_num)//健全性检查
$('.table_body').html('').load(
“{%url'更新\u项“%”?项\u数=“+item\u数”
);//首先需要为产品项创建模型
然后在views.py home函数中,从数据库加载项并将其传递给上下文
例如:
myProduct = Products.objects.all()
context = {'myList': myProduct}
return render(request, "home.html", context)
然后将此java代码添加到您的模板中,若用户想要添加项或从卡片列表中删除项,则调用它
比如:
然后
python模板:
{% for item in myList %} etc.....
现在打开views.py创建
@csrf_exempt
def add_to_card(request):
if request.method == "POST":
item_id = request.POST.get("id")
key = request.session.session_key
if key:
find = Clients.objects.filter(session_key=key)
if find.exists():
find = find.get()
card = find.card_list if find.card_list else "[]"
card = eval(card)
item_id = int(item_id)
if item_id in card:
card.remove(item_id)
else:
card.append(item_id)
find.card_list = card
find.save()
return HttpResponse("Success")
else:
raise PermissionDenied("This page is private, you not allowed to entered!!!")
else:
raise PermissionDenied("This page is private, you not allowed to entered!!!")
else:
raise PermissionDenied("This page is private, you not allowed to entered!!!")
导入python库:
from django.views.decorators.csrf import csrf_protect, csrf_exempt
现在在url.py中添加
path("client/add/card", views.add_to_card, name="Add to Card"),
您必须序列化新的产品列表项,将它们传递回ajax回调,然后迭代表体,通过JS为每个新项创建新行。这当然是可能的,但@doniyors answer更智能、更高效。谢谢。我如何更改ajax函数以返回HTML?现在,HTML正在显示在开发人员控制台中更新,但未在main.html中更新。@HC抱歉,忘记了那部分。更新了问题。我没有测试它,如果没有,请告诉我work@HC别忘了新的css类table_body
:)谢谢!它基本上是有效的。下面有几个问题:1)假设item_table的跨度为12(bootstrap),table_body现在呈现的跨度为8或9。我想知道这个方法是否会更改引导的格式?不清楚为什么格式会更改为我…2)在这个实现中,如果用户按下“上一步”按钮,用户将永远无法返回到相同的状态。有没有一种方法可以使用Ajax或其他方法来保存当前的格式nt state?我发现了渲染看起来有点滑稽的原因。出于某种原因,更新的表在代码中有/个。例如在colspan:2016年1月5日。你知道为什么以及如何修复它吗?
@csrf_exempt
def add_to_card(request):
if request.method == "POST":
item_id = request.POST.get("id")
key = request.session.session_key
if key:
find = Clients.objects.filter(session_key=key)
if find.exists():
find = find.get()
card = find.card_list if find.card_list else "[]"
card = eval(card)
item_id = int(item_id)
if item_id in card:
card.remove(item_id)
else:
card.append(item_id)
find.card_list = card
find.save()
return HttpResponse("Success")
else:
raise PermissionDenied("This page is private, you not allowed to entered!!!")
else:
raise PermissionDenied("This page is private, you not allowed to entered!!!")
else:
raise PermissionDenied("This page is private, you not allowed to entered!!!")
from django.views.decorators.csrf import csrf_protect, csrf_exempt
path("client/add/card", views.add_to_card, name="Add to Card"),