简体   繁体   English

Python / Django-如何更改模板中显示的信息?

[英]Python/ Django- How to change what information is displayed in a template?

I am working on a project that has been written in Python/ Django, and on one of the webpages, there is a table displaying some information about objects in a database. 我正在处理一个用Python / Django编写的项目,并且在其中一个网页上,有一张表显示有关数据库中对象的一些信息。

The table is displayed in a 'tabbed content' elements, and each tab displays a different table. 该表显示在“选项卡式内容”元素中,每个选项卡显示一个不同的表。 The information displayed in each table varies (ie the table has different columns) depending on the type of objects that it is displaying. 每个表中显示的信息会有所不同(即表具有不同的列),具体取决于它显示的对象的类型。

I want to remove one of the columns from one table in this 'tabbed content' element. 我想从“选项卡式内容”元素的一个表中删除列之一。

The view that returns the URL for this webpage is defined with: 返回此网页的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)

The view either returns 'reports_post_deposit.html', or 'reports_pre_deposit.html' depending on the type of request that is made, and on both pages, this currently displays a table in the 'tabbed content' area, with column headings for 'Items', 'Initial Sum', 'Latest Sum' & 'Notes'. view根据所request的类型返回“ reports_post_deposit.html”或“ reports_pre_deposit.html”,并且当前在两个页面上的“选项卡式内容”区域中均显示一个表,其中“项”的列标题为”,“初始总和”,“最新总和”和“注释”。

What I want to do, is remove the 'Latest Sum' column from the table on the 'reports_pre_deposit.html' page, but I'm not sure how to do this in Python/ Django- would I do it by changing the Django HTML file, or by changing the Python view ? 我想做的是从“ reports_pre_deposit.html”页面上的表中删除“最新总和”列,但是我不确定如何在Python / Django中做到这一点,我会通过更改Django HTML来做到这一点吗?文件,还是通过更改Python view

The report_ccis.html file that's being passed to render_to_string() is: 传递给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 %}

Edit I amended the HTML to include the new block, as suggested: 编辑我修改了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 %}

But when I now try to display the page in the browser, I get an error message that says: 但是,当我现在尝试在浏览器中显示页面时,出现一条错误消息:

TemplateSyntaxError at /costing/5547/report/ccis/ / costing / 5547 / report / ccis /处的TemplateSyntaxError

and highlights the line: 并突出显示以下行:

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

as the issue... I'm not sure why this is causing a problem? 作为问题...我不确定为什么会引起问题?

End Edit 结束编辑

The column that I want to remove will be displayed by the lines: 我要删除的列将通过以下行显示:

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

but this is also what will be displaying the rest of the columns- which I want to keep... 但这也是将显示其余列的内容,我想保留...

The reports_pre_deposit.html file itself is defined with: 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 %}

How would I remove the column from the line {{block.super}} ...? 如何从{{block.super}}行中删除该列...? Or would I need to remove it from the view that is rendering this HTML, or from the HTML file itself? 还是需要从呈现此HTML的view或HTML文件本身中将其删除?

I tried looking into the parameters that are being passed to the context variable in the report_ccis(..) view, but none of these appear to set which columns are displayed in the table. 我尝试查看了要在report_ccis(..)视图中传递给context变量的参数,但这些参数似乎都没有设置表中显示哪些列。

Can anyone point out how/ where I would change what columns are displayed? 谁能指出如何/在何处更改显示的列?

Edit 编辑

The block from pdf2_base.html that the HTML files extend , where the tables are displayed is: blockpdf2_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;">&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 %}

If I comment the 'Latest Sum' table heading with <!--th>Latest sum (£)</th--> , this obviously removes the heading from the table, but I'm not sure how I then remove the values that are displayed in that column- and it ends up just moving the 'Notes' column heading one to the left, so that this is now above the 'Latest Sum' values, and the 'Notes' column no longer has a heading. 如果我用<!--th>Latest sum (£)</th-->注释“最新总和”表标题,则显然会从表中删除该标题,但是我不确定如何删除这些值在该列中显示-并且最终仅将“注释”列的标题向左移动,因此该列现在位于“最新总和”值的上方,并且“注释”列不再具有标题。

Is there a way I can make this conditional on whether the table is displaying pre-deposit or post-deposit figures, or would I need to write a separate HTML block for each case? 有没有一种方法可以使该条件取决于表是显示pre-deposit数字还是post-deposit数字,还是我需要针对每种情况编写单独的HTML块?

Edit 编辑

The (% block content_ccis %} section now looks like this: (% 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 %}

The updated view is: 更新后的视图是:

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}} means block content of parent template. {{block.super}}表示父模板的块内容。 Because parent template in your case is "pdf2_base.html" I suppose you can find required column in "pdf2_base.html" template. 因为您的案例的父模板是“ pdf2_base.html”,所以我想您可以在“ pdf2_base.html”模板中找到必填列。

Update If you need to vary which block to show you can add context variable 'post_deposit': 更新如果您需要更改要显示的块,则可以添加上下文变量“ 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)

In template pdf2_base.html check post_deposit value and display required column: 在模板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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM