【发布时间】:2017-04-19 20:16:00
【问题描述】:
我正在处理一个用 Python/Django 编写的项目,在其中一个网页上,有一个表格显示了有关数据库中对象的一些信息。
表格显示在“选项卡式内容”元素中,并且每个选项卡显示不同的表格。每个表格中显示的信息会有所不同(即表格有不同的列),具体取决于它所显示的对象类型。
我想从这个“选项卡式内容”元素的一个表格中删除其中一列。
返回此网页 URL 的 view 定义为:
def report_ccis(request, project_id):
""" CCI items styled for pdf """
print "report_ccis() called in costing.views (1463) "
project = Project.objects.get(id=project_id)
budget = get_current_budget(project_id)
cci_total_exc = budget.cci_total_exc_vat_final
cci_grouped_items = budget.cci_items.all().order_by('project_room', 'name')
context = {
'project': project,
'cci_total_exc': cci_total_exc,
'cci_grouped_items': cci_grouped_items,
'webview': 1,
}
try: context['current_budget'] = project.budget_versions.get(current_marker=1) #For option name/date on top of pdfs
except ObjectDoesNotExist: pass
if request.GET.get('stage') == 'pd':
print "request.GET('stage') == 'pd' "
""" Render post deposit homepage """
context['html'] = render_to_string('costing/report2_ccis.html', context)
print "'render_to_sting() called with parameter: costing/report2_ccis.html "
context['active_tab'] = '4'
print "render() called with parameter: costing/reports_post_deposit.html "
return render(request, 'costing/reports_post_deposit.html', context)
else:
print "request.GET('stage') != 'pd' "
""" Render pre deposit homepage """
context['html'] = render_to_string('costing/report_ccis.html', context)
print "'render_to_sting() called with parameter: costing/report_ccis.html "
context['active_tab'] = '5'
print "render() called with parameter: costing/reports_pre_deposit.html "
return render(request, 'costing/reports_pre_deposit.html', context)
view 要么返回“reports_post_deposit.html”,要么返回“reports_pre_deposit.html”,具体取决于所创建的request 的类型,并且在两个页面上,当前都在“选项卡式内容”区域中显示一个表格,列标题为“项目”、“初始总和”、“最新总和”和“备注”。
我想要做的是从“reports_pre_deposit.html”页面上的表中删除“最新总和”列,但我不确定如何在 Python/Django 中执行此操作 - 我可以通过更改来完成吗Django HTML 文件,或者通过更改 Python view?
传递给render_to_string() 的report_ccis.html 文件是:
{% extends "pdf2_base.html" %}
{% load money_handling %}
{% block web_content %}
{% block content_overview %}
{% endblock content_overview %}
{% block content_construction %}
{% endblock content_construction %}
{% block content_schedule_of_works %}
{% endblock content_schedule_of_works %}
{% block content_report_by_class %}
{% endblock content_report_by_class %}
{% block content_ccis %}
{{block.super}}
{% endblock content_ccis %}
{% block content_payment_schedule %}
{% endblock content_payment_schedule %}
{% block agreed_variations %}
{% endblock agreed_variations %}
{% block agreed_variations_client %}
{% endblock agreed_variations_client %}
{% block agreed_variations_construction %}
{% endblock agreed_variations_construction %}
{% block unagreed_variations %}
{% endblock unagreed_variations %}
{% endblock web_content %}
编辑 按照建议,我修改了 HTML 以包含新块:
{% extends "pdf2_base.html" %} {#ERF(02/12/2016 @ 1150) Change from 'pdf_base.html - to ensure it displays all relevant variables. ' #}
{% load money_handling %}
{% block web_content %}
{% block content_overview %}
{% endblock content_overview %}
{% block content_construction %}
{% endblock content_construction %}
{% block content_schedule_of_works %}
{% endblock content_schedule_of_works %}
{% block content_report_by_class %}
{% endblock content_report_by_class %}
{% block content_ccis %}
{{block.super}}
{% endblock content_ccis %}
{# Set the block to content_ccis_pre_deposit) #}
{% block content_ccis_pre_deposit %}
{{block.super}}
{% endblock content_ccis_pre_deposit %}
{# Add details that are displayed in report2_ccis.html here too #}
{% block content_payment_schedule %}
{% endblock content_payment_schedule %}
{% block agreed_variations %}
{% endblock agreed_variations %}
{% block agreed_variations_client %}
{% endblock agreed_variations_client %}
{% block agreed_variations_construction %}
{% endblock agreed_variations_construction %}
{% block unagreed_variations %}
{% endblock unagreed_variations %}
{% endblock web_content %}
但是当我现在尝试在浏览器中显示该页面时,我收到一条错误消息:
/costing/5547/report/ccis/ 处的模板语法错误
并突出显示该行:
context['html'] = render_to_string('costing/report_ccis.html', context)
作为问题...我不确定为什么会导致问题?
结束编辑
我要删除的列将按行显示:
{% block content_ccis %}
{{block.super}}
{% endblock content_ccis %}
但这也是显示其余列的内容-我想保留...
reports_pre_deposit.html 文件本身定义为:
{% extends "costing/reports_tabbed.html" %}
{% load staticfiles utilities %}
{% block title2 %}
| Pre-deposit reports
{% endblock title2 %}
{% block page_title %}
<a id="topbar-shortcuts" data-view-url="{% url 'hub:open_sidebar' %}?app={{app.name}}&p={{project.id}}&po=1">
<span class="m-l-md">Reports</span> <img class="icon open text-sm m-l-md" src="{% static 'img/down-lt.png' %}" >
</a>
<div id="topbar-results" class="{{app.color}}" style="display:none;"></div>
{% endblock page_title %}
{% block tabs %}
{% with 'Overview, Construction budget, Schedule of works, Client choice items'|listify as tabs %}
{% for tab_name in tabs %}
{% with forloop.counter as tab %}
{% if not tab == active_tab|add:0 %}<a class="tab" href="{% url 'costing:report_tabbed' project.id %}?tab={{tab}}">{% else %}<a class="active tab">{% endif %}{{tab_name}}</a>
{% endwith %}
{% endfor %}
{% endwith %}
{% endblock tabs %}
如何从{{block.super}}...行中删除该列?或者我需要从呈现此 HTML 的 view 或 HTML 文件本身中删除它吗?
我尝试在report_ccis(..) 视图中查看传递给context 变量的参数,但这些参数似乎都没有设置表中显示的列。
谁能指出我将如何/在哪里更改显示的列?
编辑
来自pdf2_base.html 的block 表示HTML 文件extend,其中显示了表格:
{% block content_ccis %}
{% if not webview %}
<table>
<tr>
<td>
<a class="plain-link" href="{% url 'costing:home' project.id %}">Client Choice Items - please refer to 'Budget Explained' </a>
</td>
<td rowspan="2" style="text-align: right;">
</td>
</tr>
<tr>
<td>
<span class="project-name">{{project.project_name|upper}}</span>
</td>
</tr>
</table>
{% endif %}
<table class="pdf-report left">
<thead>
<tr>
<th colspan="3" style="width:400px;">Items</th>
<th>Initial sum (£)</th>
<th>Latest sum (£)</th>
<th colspan="3">Notes</th>
</tr>
</thead>
<tbody>
{% for item in cci_grouped_items %}
{% ifchanged item.project_room %}
<tr class="sub-summary">
<td>{{item.project_room}}</td>
<td></td>
<td colspan="6"></td>
</tr>
{% endifchanged %}
<tr style="padding:0.1cm;">
<td colspan="3" style="width:400px;"> {{item.name}}</td>
<td>{{item.initial_cost|money}}</td>
<td {% if item.final_cost == item.initial_cost %}style="color:blue;"{% endif %}>{{item.final_cost|money}}</td>
<td colspan="3">{{item.notes|xor}}</td>
</tr>
{% endfor %}
<tr class="end-table-section">
<td colspan="8"></td>
</tr>
<tr class="last-row">
<td colspan="3"></td>
<td>Total excluding VAT</td>
<td>{{cci_total_exc|money:'£'}}</td>
<td colspan="3"></td>
</tr>
</tbody>
</table>
<p>To help you understand the rationale behind these items, please refer to booklet 'Budget explained'. </p>
{% endblock content_ccis %}
如果我用<!--th>Latest sum (£)</th--> 注释“最新总和”表标题,这显然会从表中删除标题,但我不确定如何删除该列中显示的值 - 最终只需将 'Notes' 列标题向左移动一个,这样现在它就位于 'Latest Sum' 值之上,并且 'Notes' 列不再有标题。
有没有一种方法可以让我根据表格是显示pre-deposit 还是post-deposit 数字,或者我需要为每种情况编写一个单独的HTML 块?
编辑
(% block content_ccis %} 部分现在如下所示:
{% block content_ccis %}
{% if not webview %}
<table>
<tr>
<td>
<a class="plain-link" href="{% url 'costing:home' project.id %}">Client Choice Items - please refer to 'Budget Explained' </a>
</td>
<td rowspan="2" style="text-align: right;">
</td>
</tr>
<tr>
<td>
<span class="project-name">{{project.project_name|upper}}</span>
</td>
</tr>
</table>
{% endif %}
<table class="pdf-report left">
<thead>
<tr>
<th colspan="3" style="width:400px;">Items</th>
<th>Initial sum (£)</th>
{% if post_deposit %}
<th>Latest sum (£)</th>
{% endif %}
<th colspan="3">Notes</th>
</tr>
</thead>
<tbody>
{% for item in cci_grouped_items %}
{% ifchanged item.project_room %}
<tr class="sub-summary">
<td>{{item.project_room}}</td>
<td></td>
<td colspan="6"></td>
</tr>
{% endifchanged %}
<tr style="padding:0.1cm;">
<td colspan="3" style="width:400px;"> {{item.name}}</td>
<td>{{item.initial_cost|money}}</td>
{% if post_deposit %}
<td {% if item.final_cost == item.initial_cost %}style="color:blue;"{% endif %}>{{item.final_cost|money}}</td>
{% endif %}
<td colspan="3">{{item.notes|xor}}</td>
</tr>
{% endfor %}
<tr class="end-table-section">
<td colspan="8"></td>
</tr>
<tr class="last-row">
<td colspan="3"></td>
<td>Total excluding VAT</td>
{% if post_deposit %}
<td>{{cci_total_exc|money:'£'}}</td>
{% endif %}
<td colspan="3"></td>
</tr>
</tbody>
</table>
<p>To help you understand the rationale behind these items, please refer to booklet 'Budget explained'. </p>
{% endblock content_ccis %}
更新的视图是:
def report_ccis(request, project_id):
""" CCI items styled for pdf """
project = Project.objects.get(id=project_id)
budget = get_current_budget(project_id)
cci_total_exc = budget.cci_total_exc_vat_final
cci_grouped_items = budget.cci_items.all().order_by('project_room', 'name')
context = {
'project': project,
'cci_total_exc': cci_total_exc,
'cci_grouped_items': cci_grouped_items,
'webview': 1,
}
try: context['current_budget'] = project.budget_versions.get(current_marker=1) #For option name/date on top of pdfs
except ObjectDoesNotExist: pass
if request.GET.get('stage') == 'pd':
""" Render post deposit homepage """
context['post_deposit'] = True
print "request.GET('stage') == 'pd' "
context['html'] = render_to_string('costing/report2_ccis.html', context)
context['active_tab'] = '4'
return render(request, 'costing/reports_post_deposit.html', context)
else:
""" Render pre deposit homepage """
print "request.GET('stage') != 'pd' "
context['html'] = render_to_string('costing/report_ccis.html', context)
context['post_deposit'] = True
context['active_tab'] = '5'
return render(request, 'costing/reports_pre_deposit.html', context)
【问题讨论】: