问题描述
在Django模板中渲染ManyToManyField时,我似乎遇到了问题。我可以使其部分工作,但我不能让它正常工作,因为我想它。
首先,我有一个发票模板,显示我的数据库中的发票详细信息 p>
#invoice_details.html
{%extendsbase.html%}
{%{块内容%}
< h2>发票明细< / h2>
< div id =horizontalnav>
< a href =/ index / add_invoice>添加发票< / a>
< a href =/ index / work_orders>添加工单< / a>
< a href =/ index / add_payment>添加付款< / a>
< / div>
< ul>
< div id =list>
{invoices_list%中的发票%}
{{invoice.client}}< br />
{{invoice.invoice_no}}< br />
{{invoice.contract_info}}< br />
{{invoice.date}}< br />
{{invoice.work_orders}}< br />
{%endfor%}
< / div>
< / ul>
{%endblock%}
在我的数据库中,{{invoice.work_orders}}是如下所示显示。这是因为{{invoice.work_orders}}使用了一个manytomanyfield
< django.db.models.fields.related.ManyRelatedManager对象在0x8a811ec>
现在我尝试将{{invoice.work_orders}}更改为{{invoice.work_orders.all} }
$ b
[< Work_Order:保证支持服务>]
这类作品,但我希望它只显示保证支持服务。所以我想知道如何做到这一点。如果可能的话。
{{invoice .work_orders.all}
是 Work_Order
对象的列表。如果你想打印它们,你应该迭代清单:
{invoice.work_orders.all%中的发票所占的百分比}
{{发票}}< br />
{%endfor%}
I seem to have a problem with Django when it comes Rendering ManyToManyField in a template. I can make it work partially, but I cannot make it work properly as I want it.
Firstly I have an invoice template which displays Invoice details from my data base
#invoice_details.html
{% extends "base.html" %}
{% block content %}
<h2>Invoice Details</h2>
<div id="horizontalnav">
<a href="/index/add_invoice">Add an Invoice</a>
<a href="/index/work_orders">Add a Work Order</a>
<a href="/index/add_payment">Add Payment</a>
</div>
<ul>
<div id="list">
{% for invoice in invoices_list %}
{{invoice.client}}<br/>
{{invoice.invoice_no}}<br/>
{{invoice.contract_info}}<br/>
{{invoice.date}}<br/>
{{invoice.work_orders}}<br/>
{% endfor %}
</div>
</ul>
{% endblock %}
In my database, {{invoice.work_orders}} was displayed like the following below. This is because {{invoice.work_orders}} uses a manytomanyfield
<django.db.models.fields.related.ManyRelatedManager object at 0x8a811ec>
Now I tried to change {{invoice.work_orders}} to {{invoice.work_orders.all}} and I got this.
[<Work_Order: Assurance Support Service >]
This sort of works, but I want it to display "Assurance Support Service" only. So I am wondering how I can do this change if possible.
The content of {{invoice.work_orders.all}
is a list of Work_Order
objects.
If you want to print them, you should iterate the list:
{% for invoice in invoice.work_orders.all %}
{{invoice}}<br />
{% endfor %}
这篇关于Django的问题:显示多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!