【问题标题】:Python/ Django- How to change what information is displayed in a template?Python/Django-如何更改模板中显示的信息?
【发布时间】: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.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 %}

如果我用&lt;!--th&gt;Latest sum (£)&lt;/th--&gt; 注释“最新总和”表标题,这显然会从表中删除标题,但我不确定如何删除该列中显示的值 - 最终只需将 '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;">&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)

【问题讨论】:

    标签: python html django view


    【解决方案1】:

    你可以发送一个上下文变量来决定是否渲染block.super

    【讨论】:

    • 我不确定你的意思?这个block 是在网页上选项卡式内容的CCIs 选项卡上显示所有内容的内容 - 目前在那里显示四列 - 我仍然想显示其中三个,并且只删除一个。如果我不渲染block.super,那么该选项卡上的所有内容都会消失...
    • 对不起,也许你可以在你的父模板中有两个不同的块根据你在上下文中发送的变量来渲染块。 {% block content_ccis %} {{block.super}} {% endblock content_ccis %} {% block content_ccis2 %} {{block.super}} // 这个显示没有最后一列 {% endblock content_ccis2 %}
    【解决方案2】:

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

    【讨论】:

    • 啊-太棒了,谢谢。我查看了该文件 - 并且可以看到列标题和数据的来源 - 但如果我从那里评论/删除它,我遇到的问题是 reports_post_deposit.html 文件的数据不会显示。有没有一种方法可以根据我是查看pre-deposit 报告还是post-deposit 报告来显示此信息?如果不是,我想我需要用不同的名称复制并粘贴这个block,并使用一个用于pre-deposit,另一个用于post-deposit?我会将此表的 HTML 添加到我的 OP 中。
    • @someone 你可以在上下文 is_predeposit 中添加一些变量,然后在模板中检查 ot:{% if is_predeposit %}
    • 或者你可以简单地添加另一个块。
    • 我认为用一个变量来做这件事可能会更整洁 - 并且希望未来所做的任何更改都意味着更少的问题 - 我将在哪里定义这个?
    • @someone2088 上下文始终在视图中定义。但是,如果我正确地理解了问题,那么所有表都只有一个视图。因此,对于某些特定情况,您不能只将一些变量传递到上下文中。因此,根据我所见,您只需添加新块。
    猜你喜欢
    • 2020-08-03
    • 2022-12-09
    • 2018-10-21
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多