没有记录时弹出消息

时间:2015-07-27 11:56:15

标签: django alert

模板

{% for item in category %}
   <tr>
       <td>{{ item.Invoice_No }}</td>
       <td>{{item.Invoice_Date_Of_Issue}}</td>
       <td>{{ item.PO_No }}</td>
       <td>{{ item.Estimate_No}}</td>
       <td>{{ item.Customer_Id}}</td>
       <td>{{ item.Subject_Id}}</td>    
       <td>{{ item.Discount}}</td>
       <td>{{ item.Status }}</td>
       <td><a name="del" href='/invoiceupdate/{{item.Invoice_No}}/'>Update</a></td>
      <td><a class="delete" data-confirm="Are you sure to delete this item?" href='/invoicedelete/{{item.Invoice_No}}/'><img src="http://icons.iconseeker.com/png/fullsize/aspnet/trash-delete.png"></a></td>


   </tr>
   {% endfor %}

view.py

def Invoice_date(request):
    id = Invoice.objects.all().values_list('Customer_Id', flat=True)       
    note=request.POST.get("fromdate")
    note1=request.POST.get("todate")
    if request.method=="GET":
        form=InvoiceForm
    else:
        category=Invoice.objects.filter(Invoice_Date_Of_Issue__range=[note,note1])
        return render_to_response('test.html',locals(),context_instance=RequestContext(request))

    return render_to_response('test.html',locals(),context_instance=RequestContext(request))

我想在选定日期范围内的发票中没有记录时显示弹出消息。

有人可以帮我解决这个问题吗?提前谢谢

2 个答案:

答案 0 :(得分:1)

如果您已根据视图中的日期过滤了对象,则可以在模板中执行此操作:

{% if category %} 
    dor your for
{%else%}
    call your javascript or show a message
{%endif%}

答案 1 :(得分:1)

试试这个

{% if category %}
    {% for item in category %}
       <tr>
           <td>{{ item.Invoice_No }}</td>
           <td>{{item.Invoice_Date_Of_Issue}}</td>
           <td>{{ item.PO_No }}</td>
           <td>{{ item.Estimate_No}}</td>
           <td>{{ item.Customer_Id}}</td>
           <td>{{ item.Subject_Id}}</td>    
           <td>{{ item.Discount}}</td>
           <td>{{ item.Status }}</td>
           <td><a name="del" href='/invoiceupdate/{{item.Invoice_No}}/'>Update</a></td>
          <td><a class="delete" data-confirm="Are you sure to delete this item?" href='/invoicedelete/{{item.Invoice_No}}/'><img src="http://icons.iconseeker.com/png/fullsize/aspnet/trash-delete.png"></a></td>

   </tr>
{% empty %}
 <p>You have no records available</p>
   {% endfor %}


 {% else %}
    <p>No category records available</p>
{% endif %}

并在你的view.py中  最初将Categorey值发送为无

categorey = None

def Invoice_date(request):
    category = None
    id = Invoice.objects.all().values_list('Customer_Id', flat=True)       
    note=request.POST.get("fromdate")
    note1=request.POST.get("todate")
    if request.method=="GET":
        form=InvoiceForm
    else:
        category=Invoice.objects.filter(Invoice_Date_Of_Issue__range=[note,note1])
        return render_to_response('test.html',locals(),context_instance=RequestContext(request))

    return render_to_response('test.html',locals(),context_instance=RequestContext(request))