Django 网页 - 如何操作表中显示的信息



我有一个 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模板都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>

我想做的是,当view中的if(project.deposit_received == False)条件显示此表时,在模板中更改此表,以便表中的一列("最新总和")不会显示在表中...但是鉴于该表是使用 Djangofor循环生成和填充的,因为它根据从数据库中检索到的信息动态变化,我不确定如何做到这一点......

有没有办法在 Python 或 Django/HTML 中明确地告诉代码在满足特定条件时不显示表的某个列?

首先,您可以通过上下文将一些变量传递给模板,并检查它是存款前还是存款后。

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检查模板中循环的当前迭代。例如,如果您需要删除表中的第 3 列post_deposit您可以检查循环计数器是否等于 3 并跳过迭代:

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

最新更新