在 django 模板中定义并插入年龄

发布于 2024-10-18 12:32:02 字数 1330 浏览 1 评论 0原文

我有一个模型,视图和模板:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的痛♀有谁懂 2024-10-25 12:32:02

您需要这样做

 def age(self):
        import datetime
        return int((datetime.date.today() - self.birthday).days / 365.25  )

,因为您的生日是一个日期字段。

You need to do

 def age(self):
        import datetime
        return int((datetime.date.today() - self.birthday).days / 365.25  )

as your birthday is a DateField.

白况 2024-10-25 12:32:02

我认为您首先定义了一个方法age,并在下一行中重新分配了名称age
尝试

def calculate_age(self):
    import datetime
    return int((datetime.datetime.now() - self.birthday).days / 365.25  )
age = property(calculate_age)

一下:您可以(并且应该)在此处发布代码的相关部分,并且不要期望其他人在不同的站点上查找您的代码。

I think you first defined a method age and in the next line reassigned the name age.
Try

def calculate_age(self):
    import datetime
    return int((datetime.datetime.now() - self.birthday).days / 365.25  )
age = property(calculate_age)

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.

謌踐踏愛綪 2024-10-25 12:32:02

它也会根据您的月份显示年龄。

在 models.py 中

class Profile(models.Model):
     date_of_birth = models.DateField()

     def age(self):
            import datetime
            dob = self.date_of_birth
            tod = datetime.date.today()
            my_age = (tod.year - dob.year) - int((tod.month, tod.day) < (dob.month, dob.day))
            return my_age

在 profile.html 中

<span>Age : </span> <i> {{ profile.age }} </i>

it will display the age based on your month too.

in models.py

class Profile(models.Model):
     date_of_birth = models.DateField()

     def age(self):
            import datetime
            dob = self.date_of_birth
            tod = datetime.date.today()
            my_age = (tod.year - dob.year) - int((tod.month, tod.day) < (dob.month, dob.day))
            return my_age

in profile.html

<span>Age : </span> <i> {{ profile.age }} </i>
蓝天 2024-10-25 12:32:02

您不需要模型方法来计算年龄;您可以只使用内置模板过滤器 timesince [链接到文档]:

<span>Age: {{ person.age|timesince }}</span>

一个缺点是,如果您只想要以年为单位的年龄,目前似乎无法自定义结果字符串。例如,它通常除了返回年份之外还返回月份,例如“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]:

<span>Age: {{ person.age|timesince }}</span>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文