我有一个Django项目,我的一个views
呈现一个页面,显示一个表格,其中包含有关数据库中对象的信息。显示的对象根据view
中确定的条件而有所不同。
if
中使用view
语句来确定要呈现的页面:
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)
templates
中返回的两个view
都扩展了另一个名为reports_tabbed.html
的模板,以及report_ccis.html
&传递给report2_ccis.html
的{{1}}模板同时render_to_string
另一个名为extends
的模板。
在pdf2_base.html
中定义了在网页上显示信息的表格(无论哪一个传递给render_to_string
- report_ccis.html
或report2_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>
条件中的if(project.deposit_received == False)
条件显示时,更改模板中的此表,以便表格中的一列('最新总和' )表中没有显示......但是假设表生成了&amp;使用Django view
循环填充,因为它根据从数据库中检索到的信息动态更改,我不知道如何执行此操作...
有没有办法可以在Python或Django / HTML中明确告诉代码在满足特定条件时不显示表的某一列?
答案 0 :(得分: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)
所以现在你需要在模板中检查这个值:
{% 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 %}