在 django 模板中定义并插入年龄
我有一个模型,视图和模板:
model
class Peoples(models.Model):
l_name = models.CharField(max_length=50)
f_name = models.CharField(max_length=50)
m_name = models.CharField(max_length=50)
tab_number = models.CharField(max_length=5)
birthday = models.DateField(null=True, blank=True)
post = models.CharField(max_length=250, blank=True)
workplace = models.ForeignKey(Workplace, null=True, blank=True)
skill = models.CharField(max_length=20, blank=True)
dopuska = models.TextField(blank=True)
srok_dopuskov = models.DateField(null=True, blank=True)
photo = models.ImageField(upload_to='img',blank=True)
class Meta:
ordering = ('l_name',)
def __unicode__(self):
return self.l_name
def age(self):
import datetime
return int((datetime.datetime.now() - self.birthday).days / 365.25 )
age = property(age)
view
def view_persone(request, tab_number):
return direct_to_template(request, 'person.html', {
'persone': Peoples.objects.filter(tab_number=tab_number),
})
template
{% for person in persone %}
Возраст: <b>{{ person.age }}</b>
{% endfor %}
但是模板中应该是年龄的字段是空的。为什么?
I have a model, view and template:
model
class Peoples(models.Model):
l_name = models.CharField(max_length=50)
f_name = models.CharField(max_length=50)
m_name = models.CharField(max_length=50)
tab_number = models.CharField(max_length=5)
birthday = models.DateField(null=True, blank=True)
post = models.CharField(max_length=250, blank=True)
workplace = models.ForeignKey(Workplace, null=True, blank=True)
skill = models.CharField(max_length=20, blank=True)
dopuska = models.TextField(blank=True)
srok_dopuskov = models.DateField(null=True, blank=True)
photo = models.ImageField(upload_to='img',blank=True)
class Meta:
ordering = ('l_name',)
def __unicode__(self):
return self.l_name
def age(self):
import datetime
return int((datetime.datetime.now() - self.birthday).days / 365.25 )
age = property(age)
view
def view_persone(request, tab_number):
return direct_to_template(request, 'person.html', {
'persone': Peoples.objects.filter(tab_number=tab_number),
})
template
{% for person in persone %}
Возраст: <b>{{ person.age }}</b>
{% endfor %}
But the field in template where should be an age is empty. why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要这样做
,因为您的生日是一个日期字段。
You need to do
as your birthday is a DateField.
我认为您首先定义了一个方法
age
,并在下一行中重新分配了名称age
。尝试
一下:您可以(并且应该)在此处发布代码的相关部分,并且不要期望其他人在不同的站点上查找您的代码。
I think you first defined a method
age
and in the next line reassigned the nameage
.Try
Aside: You can (and should) post the relevant portions of the code here and not expect the others to look up your code on a different site.
它也会根据您的月份显示年龄。
在 models.py 中
在 profile.html 中
it will display the age based on your month too.
in models.py
in profile.html
您不需要模型方法来计算年龄;您可以只使用内置模板过滤器
timesince
[链接到文档]:一个缺点是,如果您只想要以年为单位的年龄,目前似乎无法自定义结果字符串。例如,它通常除了返回年份之外还返回月份,例如“37 年零 2 个月”。它还可能返回奇怪的字符串,例如“65 年 12 个月”(当出生日期至少不正好是 66 年前)。毕竟这个过滤器并不是专门用于计算一个人的年龄的。
You don't need a model method to calculate age; you can just use the built-in template filter
timesince
[link to doc]:A downside is that it seems there is currently no way to customize the resulting string if you only want the age in years. For instance, it usually returns the month in addition to years, e.g. “37 years, 2 months”. It can also return strange strings like “65 years, 12 months” (when a birth date is not at least exactly 66 years ago). This filter is not specifically intended for computing a person's age after all.