![](/img/trans.png)
[英]Python/ Django- how to update what information is displayed on a generated PDF
[英]Python/ Django- How to change what information is displayed in a template?
我正在處理一個用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
根據所request
的類型返回“ reports_post_deposit.html”或“ reports_pre_deposit.html”,並且當前在兩個頁面上的“選項卡式內容”區域中均顯示一個表,其中“項”的列標題為”,“初始總和”,“最新總和”和“注釋”。
我想做的是從“ 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 /處的TemplateSyntaxError
並突出顯示以下行:
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
變量的參數,但這些參數似乎都沒有設置表中顯示哪些列。
誰能指出如何/在何處更改顯示的列?
編輯
該block
從pdf2_base.html
該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-->
注釋“最新總和”表標題,則顯然會從表中刪除該標題,但是我不確定如何刪除這些值在該列中顯示-並且最終僅將“注釋”列的標題向左移動,因此該列現在位於“最新總和”值的上方,並且“注釋”列不再具有標題。
有沒有一種方法可以使該條件取決於表是顯示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)
{{block.super}}
表示父模板的塊內容。 因為您的案例的父模板是“ pdf2_base.html”,所以我想您可以在“ pdf2_base.html”模板中找到必填列。
更新如果您需要更改要顯示的塊,則可以添加上下文變量“ post_deposit”:
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)
context['post_deposit'] = True
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)
在模板pdf2_base.html
檢查post_deposit值並顯示必填列:
...
{% if post_deposit %}
<th>Latest sum (£)</th>
{% endif %}
...
{% if post_deposit %}
<td {% if item.final_cost == item.initial_cost %}style="color:blue;"{% endif %}>{{item.final_cost|money}}</td>
{% endif %}
...
{% if post_deposit %}
<td>{{cci_total_exc|money:'£'}}</td>
{% endif %}
...
您可以發送一個上下文變量來決定是否渲染block.super
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.