Django 日期时间字段问题
你好,我在尝试使用日期时间时遇到了 django 问题。在我的网络应用程序中,当我运行服务器时,我有一个像这样的表。
ID Owing
1 -100 (All the same value)
2 -100
3 -100
. .
. .
. .
它的一列中有发票 ID,另一列有欠款。也是一对一的关系。例如,母猪 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 %}
Hello I have been having a problem with django while trying to work with datetime. In my webapp I have a table like so when I run server.
ID Owing
1 -100 (All the same value)
2 -100
3 -100
. .
. .
. .
It has in one column Invoice id and the other owing. One-one relationship as well. sow for example owing value for 1 is 100. Unfortunately, this is where it all goes wrong because throughout column (Owing), it is giving me the owing value for ID=1. I want each ID to give me their owing value.
Here is my view. I also wonder if I may need a for loop somewhere as well.
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))
EDIT: Here is my template. The thing is the function owing is not in my models so saying {{invoices.owing}} wont work.
{% 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 %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仅获得第一个记录的
owing
值。看:首先,你拿到所有的发票:
好。但是,在
invoice_name
中,您对列表进行切片,只获取第一个元素 (invoices_list[0]
) 并从中获取名称。您对invoice_gross
执行同样的操作:看到了吗?您只能获取第一个返回元素 (
invoices_list[0]
) 的invoice_gross
。您也只能获取第一条记录的日期:然后比较该日期,并将
owing
设置为invoice_gross
,这又只是invoice_gross
第一条记录的代码>。然后将owing
传递到模板中。基本上,在模板中(或在视图中,以最简单/最好的为准),您必须循环遍历每条记录,并获取每条记录的欠值。
更新
如模板代码所示,您仅使用 first 记录的
owing
值。您可能需要编写 自定义模板过滤器计算给定记录的欠款
,因此您可以执行以下操作:You're only getting the
owing
value for the first record. Look:First, you get all the invoices:
Good. But then, in
invoice_name
, you slice the list, and only take the first element (invoices_list[0]
) and get the name from that. You do the same thing forinvoice_gross
:See? You're only getting the
invoice_gross
for the first returned element (invoices_list[0]
). You also only get the date for the first record:Then you compare that date, and set
owing
toinvoice_gross
, which, again, is only theinvoice_gross
for the first record. Then you passowing
into the template.Basically, in the template (or in the view, whichever is easiest/best), you have to loop through each record, and get the
owing
value for each individual record.Update
As shown in your template code, you are only using the value of
owing
for the first record. You'll probably need to write a custom template filter that calculatesowing
given a record, so you can do:这非常简单:您在视图中定义
owing
一次,然后在循环中一遍又一遍地引用它。根据您的观点,
owing
将始终为0
或none
(我认为您的代码缩进不正确,因为您有一个if foo:有钱=0 else:有钱=0
这是我会做的:
选项1:在视图中
您可以通过将计算出的值添加到对象实例来计算视图中每个项目的欠款。 定义一个函数来
模板:
选项 2:在模型中
获取模型中的
owing
此时模板看起来完全相同 - 通过 {{ myinstance. .欠 }}
This is pretty straight forward: You define
owing
once in your view, then refer to it in your loop over and over.According to your view,
owing
will always be0
ornone
(I think you have your code indented incorrectly, since you have anif foo: owing=0 else: owing=0
Here's what I would do:
Option 1: In the view
You can calculate owing for each item in your view by adding the calculated value to your object instance. It's a python object you can do anything to.
Template:
Option 2: In the model
Define a function to get
owing
in the model.At which point the template would look exactly the same -- access owing by {{ myinstance.owing }}