我使用的是Django 1.10.7和Python 3.6.1。首先是我的代码,然后是我的问题。基于Django模型字段计算的模板变量
我的模型看起来是这样的:
class Thing(models.Model):
user = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
start_date = models.DateTimeField(
default=timezone.now)
cost_per_day = models.DecimalField(max_digits=6, decimal_places=2)
这里是我的观点:
def something_list(request):
things =Thing.objects.filter(start_date__lte=timezone.now()).order_by('start_date')
return render(request, 'myApp/something_list.html', {'things': things})
def thing_detail(request, pk):
thing = get_object_or_404(Thing, pk=pk)
return render(request, 'myApp/thing_detail.html', {'thing': thing})
我对这些视图,在这里我使用的模板标签来显示两个变量模板块。例如:
<h2 class="title"><a href="{% url 'thing_detail' pk=thing.pk %}">{{ thing.title }}</a></h2>
<p>User: {{ thing.user }}</p>
<div class="date">
<p>Start Date: {{ thing.start_date }}</p>
</div>
<p>Cost per day: ${{ thing.cost_per_day }}</p>
那么这里发生了什么?
网络用户可以输入我所说的任何数量的“事情”,包含4个字段:用户,标题,开始日期和每日费用。我有两个计算,我想做,并将其呈现在模板中。
问题1)用户需要查看自从最初进入开始日期以来经过了多少天。计算将是当前时间/日期(现在)和开始日期的减法。我可以用这里显示的Django的timesince功能来做到这一点:
<button type="button" class="btn btn-info">{{ thing.quit_date|timesince }}</button>
这使得有多少天,时间已经过去了 - 让称它为“时间流逝” - 完美。问题在于我的下一次计算。
问题2)我需要显示'经过时间'(如上所示)乘以当前模型实例中的每日费用变量的计算。为了清楚起见,我们假设开始日期是30.5天前,每天的成本是5.00美元。我需要乘以30.5乘以5.00美元。我很想简单地乘以模板标签,但我明白这不是它的工作原理。例如:
{{ thing.quit_date|timesince }} * {{ thing.cost_per_day }}
有没有办法抓住这个timesince计算的结果... {{thing.quit_date | timesince}} ...作为一个变量?我可能无法使用timesince功能进行此计算。
再一次,我最终需要做的是将“流逝的时间”乘以每天的费用,如下所示:(时间现在 - 开始日期)*(每日费用)。
我不知道该怎么做models.py或views.py。我正在寻找使用Django和Python 3.6来做到这一点的最佳做法。我是一个新手,我一整天都在搜索我的特定问题的答案。
预先感谢,请让我知道如果我需要提供更多的信息!
UPDATE
这里是根据建议,我更新的模式,虽然不工作(在timesince财产的工作需要):
from django.db import models
from django.utils import timezone
from datetime import datetime
class Thing(models.Model):
quitter = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
quit_date = models.DateTimeField(default=timezone.now)
cost_per_day = models.DecimalField(max_digits=6, decimal_places=2)
notes = models.TextField()
@property
def timesince(self):
"Time since quit date"
now = datetime.now()
then = self.quit_date
return (now - then)
@property
def savings(self):
"Savings since quitting"
return self.timesince * self.cost_per_day
这是我更新的模板:
<h4>Here are some calculations for this information...</h4>
<p>Days since you quit:</p>
<button type="button" class="btn btn-info">{{ thing.timesince }}</button>
<hr>
<p>How much money you have saved:</p>
<button type="button" class="btn btn-info">{{ thing.savings }}</button>
我肯定的问题在于减去日期。任何对此的洞察都将非常有帮助。模板变量{{}}正在工作,但模型中缺少一块。顺便说一下,当我将“天”连接到任何日期时间变量时,它会在我的错误中说明错误,并告诉我错误。也许有使用DateTimeField和datetime.now()实例的问题?
您是否考虑过自定义模板标记? https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/ –
不可否认,在我的学习中,我现在有点被自定义模板标签吓倒了。我感谢你的建议!我将按照以下建议尝试@property。 –