2016-12-05 55 views
0

我正在編寫一個已經用Python/Django編寫的項目,並且在其中一個網頁上,有一個表格顯示一些關於數據庫中對象的信息。Python/Django-如何更改在模板中顯示的信息?

該表格顯示在「標籤式內容」元素中,並且每個標籤顯示不同的表格。每個表格中顯示的信息都不相同(即表格具有不同的列),具體取決於它顯示的對象的類型。

我想從這個「標籤式內容」元素中的一個表中刪除一列。

view用於返回該網頁的URL與定義:

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」,或取決於由request類型「reports_pre_deposit.html」,並且在這兩個頁面上,這當前都會在「標籤式內容」區域中顯示一個表格,列標題爲「項目」,「初始金額」,「最新金額」&'注意'。

我想要做的是從'reports_pre_deposit.html'頁面的表格中刪除'Latest Sum'列,但我不確定如何在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 %} 

但是當我現在嘗試要在瀏覽器中顯示頁面,我收到一條錯誤消息:

TemplateSyntaxError在/成本/ 5547 /報告/ CCIS/

,並強調該行:

context['html'] = render_to_string('costing/report_ccis.html', context) 

的問題......我不知道爲什麼,這是造成問題?

末編輯

,我想刪除將由行中顯示的列:

{% block content_ccis %} 
    {{block.super}} 
{% endblock content_ccis %} 

但這也什麼都會顯示這是我想要的columns-其餘

{% 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 %} 
012:保持...

reports_pre_deposit.html文件本身與定義

我將如何從行{{block.super}} ...刪除列?或者我需要從正在呈現此HTML的view或HTML文件本身中刪除它?

我試着查看傳遞給report_ccis(..)視圖中的context變量的參數,但這些參數似乎都沒有設置表中顯示的列。

任何人都可以指出我將如何/在哪裏更改顯示的列?

編輯

pdf2_base.htmlblock的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;">&nbsp;&nbsp;&nbsp;&nbsp;{{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;">&nbsp;&nbsp;&nbsp;&nbsp;{{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) 

回答

0

您可以發送一個上下文變量來決定是否呈現塊。超或沒

+0

我不確定你是什麼意思?這個「塊」是顯示網頁上標籤內容的「CCIs」選項卡上的所有內容 - 當前有四列顯示 - 我仍然想要顯示其中的三個,只刪除一個。如果我不渲染'block.super',那麼該選項卡上的所有內容都會消失... – someone2088

+0

對不起,也許您可​​以在父模板中有兩個不同的塊,根據您在上下文中發送的變量渲染塊。 {%block content_ccis%} {{block.super}} {%endblock content_ccis%} {%block content_ccis2%} {{block.super}} //此顯示沒有最後一列 {%endblock content_ccis2%} – rsb

1

{{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 %} 
... 
+0

啊,輝煌的,謝謝。我查看了該文件,並可以看到列標題和數據來自何處,但是如果我在此處註釋/刪除該問題,則存在的問題是數據不會顯示在「reports_post_deposit.html」文件。有沒有辦法讓我可以根據我是否查看「預存款」報告或「存款後」報告來顯示這些信息?如果不是,我想我需要複製並粘貼這個''''塊名稱不同的名稱,並使用'pre-deposit'和另一個'post-deposit'?我將把這個表格的HTML添加到我的OP中。 – someone2088

+0

@someone你可以添加一些變量到上下文is_predeposit,然後在模板中檢查ot:{%if is_predeposit%} – neverwalkaloner

+0

或者你可以簡單地添加另一個塊。 – neverwalkaloner

相關問題