【发布时间】:2011-01-07 16:10:06
【问题描述】:
您好,我在尝试使用 datetime 时遇到了 django 的问题。在我的 web 应用程序中,当我运行服务器时,我有一个这样的表。
ID Owing
1 -100 (All the same value)
2 -100
3 -100
. .
. .
. .
它有一列发票编号和另一列欠款。一对一的关系也是如此。例如,母猪 1 的欠款值为 100。不幸的是,这一切都出错了,因为在整个列 (Owing) 中,它给了我 ID=1 的欠款值。我希望每个 ID 都能给我他们应得的价值。
这是我的看法。我也想知道我是否也需要一个 for 循环。
def homepage(request):
invoices_list = Invoice.objects.all()
invoice_name = invoices_list[0].client_contract_number.client_number.name
invoice_gross = invoices_list[0].invoice_gross
payment_date = invoices_list[0].payment_date
if payment_date <= datetime.now():
owing = invoice_gross
if payment_date > datetime.now():
owing = 0
else: owing= 0
return render_to_response(('index.html', locals()), {'invoices_list': invoices_list ,'invoice_number':invoice_number, 'invoice_name':invoice_name, 'invoice_gross':invoice_gross, 'payment_date':payment_date, 'owing': owing}, context_instance=RequestContext(request))
编辑:这是我的模板。问题是我的模型中没有所欠的功能,所以说 {{invoices.owing}} 不起作用。
{% for invoices in invoices_list %}
<tr>
<td>{{invoices.invoice_number}}</td>
<td>{{invoices.invoice_contact}}</td>
<td>{{invoices.client_contract_number}}</td>
<td>{{invoices.payment_date|date:"d M Y"}}</td>
<td>{{invoices.invoice_gross}}</td>
<td>{{owing}}</td>
{% endfor %}
【问题讨论】:
标签: django datetime views models