Django-如何将另一个字段添加到网页中显示的表中

时间:2016-11-09 17:07:55

标签: python html django

我正在调试Django项目,并希望在网页的表格中显示数据库中的另一个字段。

当前在网页中显示的表格的Django HTML是:

<table class="multisection pipeline left">
        <tr class="sub-summary">
             <th colspan="4"><a href="?detailed_status={{detailed_status}}"><h3 class="p-l-sm">{{detailed_status_str}}</h3></a></th>
             {% if total_i %}<th>Initial exc VAT: {{total_i|money:"£"}}</th>{% endif %}
             {% if total_u %}<th>Latest exc VAT: {{total_u|money:"£"}}</th>{% else %}
             <th></th>
             {% endif %}
        </tr>
    </table>
    <table class="multisection pipeline left m-b-xl">
        <tr class="summary">
            <th style="width: 3em;"></th>
            {% for field in fields %}
                <th class="text-sm p-l-sm p-t-sm p-b-sm" style="width:{{widths|getval:forloop.counter0}}">
                {% if field.1 %}
                    {% if sort == field.0 and not reverse %}
                        <a href="?sort=-{{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a>
                    {% else %}
                        <a href="?sort={{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a>
                    {% endif %}
                {% else %}
                    {{field.0}}
                {% endif %}
                </th>
                {# Make all have the same number of columns (8) #}
                {% if forloop.last %}
                    {% for i in ',,,,,,,,' %}
                        {% if forloop.counter|add:forloop.parentloop.counter0 < 11 %}
                            <th>&nbsp;</th>
                        {% endif %}
                    {% endfor %}
                    {% if detailed_status == "ds4"|ds %}
                        <th></th>
                    {% endif %}
                {% endif %}
            {% endfor %}
        </tr>
        {% with user.employee.full_name|is:'Nick Ross' as summary_link %}

        {% for project in projects %}
            <tr data-project-id="{{project.id}}" class="{% cycle 'odd' 'even' %}{% if project.office == 2 %} col{% endif %}">
                {% with initial_details=project.initial_details survey=project.survey  %}
                    {# Open lightbox #}
                    <td>
                        {% if user.is_superuser %}
                            <a class="gallery-loader" data-project-id="{{project.id}}"><i class="icon info"></i></a>

                            {% if forloop.first and first_table %}
                                <div id="iframe_gallery_wrap">

                                    <a href="#p1" class="gallery">
                                        <div id="p1">
                                            <iframe class="lightbox-content" src="{% url 'projects:description' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
                                        </div>
                                    </a>
                                    <a href="#p2" class="gallery">
                                        <div id="p2">
                                            <iframe class="lightbox-content" src="{% url 'projects:survey' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
                                        </div>
                                    </a>
                                    <a href="#p3" class="gallery">
                                        <div id="p3">
                                            <iframe class="lightbox-content" src="{% url 'projects:preconcept_notes' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
                                        </div>
                                    </a>
                                    <a href="#p4" class="gallery">
                                        <div id="p4">
                                            <iframe class="lightbox-content" src="{% url 'projects:concept' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
                                        </div>
                                    </a>
                                    <a href="#p5" class="gallery">
                                        <div id="p5">
                                            <iframe class="lightbox-content" src="{% url 'projects:handover' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
                                        </div>
                                    </a>
                                </div>
                            {% endif %}
                        {% endif %}

                    </td>
                    {# Standard columns #}
                    {% with project.team as team %}

                    <td><a href="{% if summary_link %}{% url 'projects:summary' project.id %}{% else %}{% url 'projects:details' project.id %}{% endif %}">{{project.project_name}}</a></td>
                    <td>{{project.town|xor}}</td>
                    <td>{{project.postcode}}</td>
                    <td>{{project.client.full_name_a|xor}}</td>
                    {# Status-specific columns #}
                    {% if detailed_status == "ds1"|ds or detailed_status == "ds2"|ds %} {# New project leads #}
                        <td>{{project.created|date:"d/m/y"}}</td>
                        <td>{{initial_details.site_visit.date|date:"d/m/y"}}</td>
                        <td>{{initial_details.discussed_budget|money:'£'|xor}}</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    {% elif detailed_status == "ds3"|ds %} {# CDI Sent #}
                        <td>{{project.cdi.sent|date:"d/m/y"}}</td>
                        <td>{{initial_details.discussed_budget_updated|money:"£"|xor}}</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    {% elif detailed_status == "ds4"|ds %} {# CDI Signed #}
                        <td>{{team|getval:'Designer'|xor}}</td>
                        <td>{{project.budget_overview.current_exc_vat|money:"£"|xor}}</td>
                        <td>{{project.survey.meeting.date|date:"d/m/y"}}</td>
                        <td>{% for emp in project.survey.get_surveyors %}{{emp}}, {% endfor %}</td>
                        <td>{{initial_details.estimated_start|date:"m/Y"}}</td>
                        <td>&nbsp;</td>
                    {% elif detailed_status == "ds5"|ds %} {# Survey completed #}
                        <td>{{team|getval:'Designer'|xor}}</td>
                        <td>{{project.budget_overview.updated_exc_vat|money:"£"|xor}}</td>
                        <td>{{initial_details.preconcept_meeting.date|date:"d/m/y"}}</td>
                        <td>{{initial_details.first_presentation.presentation_date|date:"d/m/y"}}</td>
                        <td>{{initial_details.estimated_start|date:"m/Y"}}</td>
                    {% elif detailed_status == "ds6"|ds %} {# Tendering/costing drawings #}
                        <td>{{team|getval:'Designer'|xor}}</td>
                        <td>{{project.budget_overview.updated_exc_vat|money:"£"|xor}}</td>
                        <td>{{initial_details.estimated_start|date:"m/Y"}}</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    {% elif detailed_status == "ds7"|ds %} {# Post presentation #}
                        <td>{{team|getval:'Designer'|xor}}</td>
                        <td>{{project.budget_overview.updated_exc_vat|money:"£"|xor}}</td>
                        <td>{{project.chance}}{% if project.chance %}%{% endif %}</td>
                        <td>{{initial_details.last_presentation.date|date:"d/m/y"}}</td>
                        <td>{{initial_details.estimated_start|date:"m/Y"}}</td>
                    {% else %}
                        {# Standard-ish columns #}
                        <td>{{team|getval:'Architect'|xor}}</td>
                        <td>{{project.budget_overview.updated_exc_vat|money:"£"|xor}}</td>
                        <td>{{project.live_date|date:"d/m/y"}}</td>
                        <td>{{initial_details.desired_start|date:"d/m/y"}}</td>
                        <td>&nbsp;</td>
                    {% endif %}
                    <!--ERF(09/11/2016 @ 1540) Add an 'elif' for the site manager (ds9 is the 'Live Project' table) -->
                    {% if detailed_status == "ds9"|ds %}
                        <td>{{team|getval:'Site manager'|xor}}</td>
                        <!--td>{{project.site_manager}}</td-->
                    {% endif %}

                        {% if user.is_staff or user.is_superuser %}
                            <td>
                                <a class="delete double-check m-l-xl" data-view-url="{% url 'projects:delete' project.id %}"><img class="icon" src="{% static "img/bin.png" %}"></a>
                            </td>
                        {% endif %}
                        <td data-view-url="{% url 'projects:update_detailed_status' project.id %}" style="width:10em;">
                            {{detailed_status_changer.visible_fields.0}}
                        </td>
                    {% endwith %}

                {% endwith %}

            </tr>
        {% endfor %}
        {% endwith %}

    </table>

,目前显示的是包含以下列标题的表格:

项目名称,城镇,邮政编码,联系人姓名

这些列标题是属于类Project的字段,它在models.py文件中定义:

class Project(models.Model):
    ...
    ds2 = 20
    ...
    ds9 = 120
    ...
    ds13 = 200

    DETAILED_STATUS_CHOICES = (
        ...
        ds9, 'Live project'),
        ...
    )

    # Several form fields added here, e.g. 
    employees = models.ManyToManyField(Employee, through=ProjectEmployee)
    client = models.ForeignKey(Client, on_delete=models.SET_NULL, null=True, blank=True)

    # Several 'def's here, e.g.
    def team(self):
        employees = self.assigned.all()
        ...
        sm = [person for person in employees if person.role==Role.SM]
        ...
        return{
            ...
            'Site manager': sm[0].employee.first_name if sm else '',
            ...
        }

    # Several `@property` definitions here, e.g.
    @property
    def estimated_compeltion(self):
        ...

我想在网页上的表格中显示数据库中的Site manager字段 - 我尝试将其添加到Python代码中,如上所示,但这对于什么没有影响显示在网页上,即使它看起来与显示数据库中所有其他字段的方式完全相同。

我在这里缺少什么?我希望Django HTML能够在Python代码中显示我告诉它的每个字段,因为我使用forloop来获取字段...为什么我看不到'Site我已经添加到HTML网页表格中的代码的经理字段了吗?

1 个答案:

答案 0 :(得分:0)

使用<td/>

project.site_manager

虽然您的Project模型似乎没有这样的字段,但这就是为什么没有显示的原因。

也许您可以在project_manager类中为Project创建一个getter,如下所示:

def get_site_manager(self):
    sm = [person for person in self.assigned.all() if person.role==Role.SM]
    return sm[0].employee.first_name if sm else ''

并在模板中使用它:

<td>{{project.get_site_manager()}}</td>