2016-12-06 55 views
0

我有一個Django項目,並且其中一個views呈現一個頁面,該頁面顯示一個包含數據庫中對象信息的表格。顯示的對象根據view中確定的標準而有所不同。Django網頁 - 如何操縱表中顯示的信息

view用來確定渲染哪些頁面if聲明:

if request.GET.get('stage') == 'pd':   
    print "request.GET.get('stage') == 'pd' " 
    print "render_to_string() called with parameter: costing/report2_ccis.html" 
    context['html'] = render_to_string('costing/report2_ccis.html', context) 
    context['active_tab'] = '4' 

    if(project.deposit_received == True): 
     print "costing/reports_post_deposit.html is the page being rendered (project.deposit_received = true)... " 
     context['post_deposit'] = True 
     print "context['post_deposit']: ", context['post_deposit'] 
     return render(request, 'costing/reports_post_deposit.html', context) 
    elif(project.deposit_received == False): 
     print "costing/reports_pre_deposit.html is the page being rendered (project.deposit_received = False)..." 
     context['post_deposit'] = False 
     print "context['post_deposit']: ", context['post_deposit'] 
     return render(request, 'costing/reports_pre_deposit.html', context) 
else: 
    print "request.GET.get('stage') != 'pd' " 
    print "render_to_string() called with parameter: costing/report_ccis.html" 
    context['html'] = render_to_string('costing/report_ccis.html', context) 
    context['active_tab'] = '5' 
    if(project.deposit_received == True): 
     print "costing/reports_post_deposit.html is the page being rendered (project.deposit_received = true)..." 
     context['post_deposit'] = True 
     print "context['post_deposit']: ", context['post_deposit'] 
     return render(request, 'costing/reports_post_deposit.html', context) 
    elif(project.deposit_received == False): 
     print "costing/reports_pre_deposit.html is the page being rendered (project.deposit_received = false)..." 
     context['post_deposit'] = False 
     print "context['post_deposit']: ", context['post_deposit'] 
     return render(request, 'costing/reports_pre_deposit.html', context) 

無論在view返回templates的擴展另一個模板稱爲reports_tabbed.html,並傳遞給了report_ccis.html & report2_ccis.html模板render_to_string查看extends另一個模板名爲pdf2_base.html

多數民衆贊成顯示在網頁上(哪一個被傳遞給render_to_string - report_ccis.htmlreport2_ccis.html)信息的表在pdf2_base.html定義的:

<table class="pdf-report left"> 
    {% for payment_details in payments_details %} 
     {% if forloop.first %} 

     <thead> 
      <tr> 
       {% for detail in payment_details %} 
        <th>{{ detail.0 }}</th> 
       {% endfor %} 
       <th></th> 
      </tr> 
     </thead> 
     {% endif %} 
    <tbody> 
     {% if 0 %} 
      <tr class="end-table-section"></tr> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
      </tr> 
     {% endif %} 


      <tr {% if forloop.last %}class="end-table-section last-row"{% endif %}> 


      {% for detail in payment_details %} 
       <td {% if detail.0 == 'Gross payment (£)' %}class="payment-{{detail.2}}"{% endif %}> 
       {% if detail.1 and detail.0 != 'Date paid' and detail.0 != 'Date' %} 
       {% if detail.0 == 'VAT (£)' or detail.0 == 'Scheduled payment (£)' or detail.0 == 'Gross payment (£)' %} 
        {{ detail.1|money }} 
       {% else %} 
        {{ detail.1 }} 
       {% endif %} 
       {% elif detail.1 %} 
       {{ detail.1|date:'d-M' }} 
       {% endif %} 
       </td> 
      {% endfor %} 
       <td></td> 
      </tr> 
      {% if 0 %} 
       <tr class="end-table-section"></tr> 
        <td></td> 
        <td></td> 
        <td></td> 
        <td></td> 
        <td></td> 
        <td></td> 
       </tr> 
      {% endif %} 
     {% endfor %} 
     <tr class="end-table-section"> 
      <td>Gross payment now due:</td> 
      <td>{{gross_payment_due|money:'£'}}</td> 
      <td>Current contract sum</td> 
      <td>Exc VAT {{ latest_total_exc|money:'£' }}</td> 
      <td></td> 
      <td>Bank</td> 
      <td>Handelsbanken</td> 
     </tr> 
     <tr> 
      <td></td> 
      <td></td> 
      <td>Total payments made</td> 
      <td>Exc VAT {{total_paid_exc|money:'£'}}</td> 
      <td></td> 
      <td> acc</td> 
      <td></td> 
     </tr> 
     <tr> 
      <td></td> 
      <td></td> 
      <td>Outstanding contract sum</td> 
      <td>Exc VAT {{outstanding_exc|money:'£'}}</td> 
      <td>Inc VAT {{outstanding_inc|money:'£'}}</td> 
      <td>Sort Code</td> 
      <td></td> 
     </tr> 
     <tr class="last-row"> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td colspan="2">Please ensure address is put as reference</td> 
     </tr> 
    </tbody> 
</table> 

我想要做的,是在改變這個表viewif(project.deposit_received == False)條件顯示的模板,以便表格中的一列('最近總和')不會顯示在表格中...但是,假設使用Django生成了表格& for循環,因爲它根據檢索到的信息動態變化從數據庫,我不知道如何做到這一點...

有沒有一種方法,我可以明確地告訴代碼,無論是在Python或Django/HTML不顯示某一列的表時,一個特定的條件得到滿足?

回答

0

首先,您可以通過上下文向模板傳遞一些變量,並檢查它是預存還是後置。

if(project.deposit_received == True): 
    context['post_deposit'] = True 
    return render(request, 'costing/reports_post_deposit.html', context) 
elif(project.deposit_received == False): 
    context['post_deposit'] = False 
    return render(request, 'costing/reports_pre_deposit.html', context) 

所以現在yoou需要在模板中檢查該值:

{% if post_deposit %} 
    do_something 
{% else %} 
    do_something_else 
{% endif %} 

,最後一個則可以使用forloop.counter檢查週期的當前迭代的模板。例如,如果你需要刪除post_deposit表第3列,你可以檢查循環計數器等於3,並跳過迭代:

{% for detail in payment_details %} 
    {% if not post_deposit or forloop.counter != 3 %} 
     <th>{{ detail.0 }}</th> 
    {% endif %} 
{% endfor %} 
+0

嘿,感謝您的更新,我給這個中間人,雖然它已取消當deposit_received變量爲False時,該列也會在變量爲True時將其刪除,因此該列現在不再顯示了......我猜測我需要查看一些內容關於現在的條件 - 但我的調試顯示每次我加載頁面時,這個變量的值 - 我是否加載了一個項目,該項目將該變量的值設置爲「True」或「False」,列是永遠不會顯示... – someone2088

+0

@ someone2088你檢查模板中的值?只需添加到模板「{{post_deposit}}」中即可。這會告訴你'post_deposit'是否改變。如果你看到該變量正在改變,請編輯你的問題中的代碼,我會看看。 – neverwalkaloner

+0

我看不到我的模板的任何輸出(甚至在向其添加'{{post_deposit}}'之後,但我的'view'中的調試確實顯示'post_deposit'的值正在改變 - 無論它是否它是真或假,該列現在不再顯示在表中了... – someone2088