简体   繁体   中英

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.

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:

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'.

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 ?

The report_ccis.html file that's being passed to render_to_string() is:

{% 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:

{% 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/

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:

{% 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}} ...? Or would I need to remove it from the view that is rendering this HTML, or from the HTML file itself?

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.

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:

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

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?

Edit

The (% block content_ccis %} section now looks like this:

{% 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. Because parent template in your case is "pdf2_base.html" I suppose you can find required column in "pdf2_base.html" template.

Update If you need to vary which block to show you can add context variable '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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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