上下文未传递给包含的模板

问题描述

我正在尝试使用上下文参数将模型从我的视图传递到模板。 问题是上下文没有传递给包含的模板,它可以在其他任何地方工作,只是不在包含的模板中(在应用 ajax 之前它工作正常)。

这是我的模板 (gifts_template.html):

<form id="form_filter_gifts">
{% csrf_token %}
<div class="row">
    <div class="col">
        <select class='form-select' name="s_filter_gifts_by_date" id="s_filter_gifts_by_date">
            <option value="" selected>Show Gift Cards From</option>
            {% for year,month_list in gifts_years_months.items %}
                {% for month in month_list %}
                    {% if gifts_selected_year == year and gifts_selected_month == month %}
                        <option value="{{ year }}-{{ month }}" selected>{{ year }}-{{ month }}</option>
                    {% else %}
                        <option value="{{ year }}-{{ month }}">{{ year }}-{{ month }}</option>
                    {% endif %}
                {% endfor %}
            {% endfor %}
        </select>
    </div>
    <div class="col">
        <input type="submit" name="btn_filter_gifts_date" class="btn btn-light" value='Filter'>
    </div>
</div>
<span>Cool!</span>
{% if gifts %}
<span>Cool!</span>
<br style="line-height: 0;" />
<table class="table table-light table-striped">
    <thead>
      <tr>
        <th scope="col">Date</th>
        <th scope="col">Gift Amount</th>
        <th scope="col">Tax</th>
      </tr>
    </thead>
    <tbody>
        {% for gift in gifts %}
        <tr>
            <td>{{ gift.date }}</td>
            <td>{{ gift.gift_money|floatformat }}</td>
            <td>{{ gift.gift_tax|floatformat }}</td>
        </tr>
        {% endfor %}
        
    </tbody>
  </table>
  {% endif %}

这是相关的 html 包含器:

<!--Balance Modal-->
<div class="modal fade" id="balance_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content bg-secondary">
            <div class="modal-header">
                <div class="modal-title display-4" id="balance_exampleModalLongTitle">Balance ${{ user_balance|floatformat }}</div>
            </div>
            <div class="modal-body">
                {% include 'gifts_table.html' %}

                <form method="POST" action="" id='balance'>
                    {% csrf_token %}
                    <div class="form-group inner-addon">
                        <input name="txt_balance_add" id="txt_balance_add" type="text" class="form-control" placeholder="Add This Amount For Balance" autocomplete="false">
                        <i class="fa fa-dollar-sign fa-lg fa-fw" aria-hidden="true"></i>
                    </div>
                    {{ giftform.date }}
                    <div class="row">
                        <div class="col">
                            <div class="form-group inner-addon">
                                {{ giftform.gift_money }}
                                <i class="fa fa-gift fa-lg fa-fw" aria-hidden="true"></i>
                            </div>
                        </div>
                        <div class="col">
                            <div class="form-group inner-addon">
                                {{ giftform.gift_tax }}
                                <i class="fa fa-dollar-sign fa-lg fa-fw" aria-hidden="true"></i>
                            </div>
                        </div>
                    </div>
                    {{ giftform.user }}
                    <button type="submit" class="btn btn-primary" name="btn_change_balance">Make Changes</button>
                </form>
                {{giftform.errors}}
            </div>
        </div>
    </div>
</div>

这是ajax:

$(document).on("submit","#form_filter_gifts",function(e){
    e.preventDefault();

    $.ajax({
        url: "{% url 'filter_gifts' %}",type: "GET",data: {date:$("#s_filter_gifts_by_date").val()}
    })
    .done(function(response){
        $("#balance_modal").modal('show');
    })
    .fail(function(xhr,status,error){
        var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
    })
})

这是视图:

@csrf_exempt
def filter_gifts(request):
if request.method == 'GET':
    context = {}
    date = str(request.GET['date'])

    if date:
        year = int(date.split('-')[0])
        month = int(date.split('-')[1])

        context['gifts'] = Gift.objects.filter(user=request.user.id,date__year=year,date__month=month)
        context['gifts_selected_year'] = year
        context['gifts_selected_month'] = month

        print(context)
    else:
        context['gifts'] = None

return render(request,'gifts_table.html',context)

“酷!”用于调试目的,我只能看到一个(提交后我需要看到2个)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)