在AJAX调用之后,如何更新django模板上下文变量? [英] How do you update a django template context variable after an AJAX call?

查看:366
本文介绍了在AJAX调用之后,如何更新django模板上下文变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表Product,其中显示了一组产品的信息.

I have a table Product that shows information of a group of products.

    <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调用来更新表.

I am using the below Ajax call you update the table.

$(document).ready(function(){

// Submit post on submit
$('.item_num').on('click', function(event){
    event.preventDefault();
    var item_num = $(this).attr('id');
    update_item(item_num);
});

function update_item(item_num) {
    console.log(item_num) // sanity check
    $.ajax({
        type: 'GET',
        url:'update_items', 
        data: { 'item_num': item_num },

        success: function(result){
            console.log(result);
            ???$('item_table').product_list = result;???
        },
... more code

如何在我的Ajax调用中使用结果"更新变量product_list?

How do I update the variable product_list with 'result' from my Ajax call?

这应该更新表格吗?

谢谢

推荐答案

您不能采用这种方式.更好的方法是通过ajax加载html的那部分.

you cannot this way. Better way would be to load that part of your html via ajax.

您的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>

table_body.html:

table_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 %}

您的ajax看起来像这样:

your ajax would look like this:

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)

您在此处使用 load()

这篇关于在AJAX调用之后,如何更新django模板上下文变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆