我有一个Django项目,其中一个views呈现了一个页面,该页面显示了一个表,其中包含有关数据库中对象的信息。根据view中确定的条件,显示的对象会有所不同。
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的模板,以及传递给render_to_string视图的report_ccis.html & report2_ccis.html模板都扩展了另一个名为pdf2_base.html的模板。
在pdf2_base.html中,显示网页上信息的表(无论是传递给render_to_string- report_ccis.html还是report2_ccis.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>我想要做的是,当view中的if(project.deposit_received == False)条件显示该表时,在模板中更改该表,这样表中的一列('Latest Sum')就不会显示在表中...但是考虑到这个表是使用Django for循环生成和填充的,因为它会根据从数据库中检索到的信息动态变化,所以我不知道该怎么做……
有没有一种方法可以显式地告诉代码,无论是在Python中还是在Django/ HTML中,当满足特定条件时不显示表的特定列?
发布于 2016-12-07 00:20:40
首先,您可以通过上下文将一些变量传递给模板,并检查它是存放前还是存放后。
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,并跳过迭代:
{% for detail in payment_details %}
{% if not post_deposit or forloop.counter != 3 %}
<th>{{ detail.0 }}</th>
{% endif %}
{% endfor %}https://stackoverflow.com/questions/40997683
复制相似问题