Search code examples
pythondjangodatafield

Define age in django template using actual datatime and birthday with datafield


i'm begginer at django and trying to display age for every user in my base of users.

Here's my code:

models.py:

class Cv(models.Model):
    author = models.ForeignKey('auth.User')
    name = models.CharField(max_length=25, null = True)
    surname = models.CharField(max_length=25, null = True)
    address = models.CharField(max_length=100, blank=True)
    telephone = models.IntegerField()
    birth_date = models.DateField(blank=True, null=True)
    email = models.EmailField(max_length=50, null=True)
    skills = models.TextField(null=True)
    specialization = models.CharField(max_length=30, blank=True, null=True)
    interests = models.TextField(blank=True, null=True)
    summary = models.TextField(blank=True, null=True)
    thumbnail = models.FileField(upload_to=get_upload_file_name, blank=True)




    def zapisz(self):
        self.save()

    def __str__(self):
        return self.surname

template.html:

{% block base %}
<div class="vvv">
    <h2>Base of users</h2><hr>
    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
          <tr>
            <th>Nr.</th>
            <th>Full Name</th>
            <th>Specialization</th>     
            <th>Age</th>
            <th>E-mail</th>
          </tr>
         </thead>
         <tbody>
          {% for cv in cvs %}
              <tr>
                <td>{{forloop.counter}}.</td>
                <td><a href="{% url "proj.views.cv_detail" pk=cv.pk %}">{{cv.name}} {{cv.surname}}</a></td>
                <td>{{cv.specialization}}</td>      
                <td>{{ cv.age }} </td>
                <td>{{cv.email}}</td>
              </tr>
          {% endfor %}
          </tbody>
    </table><br>


</div>
{% endblock %}

views.py:

@login_required
def base_cv(request):

    cvs = Cv.objects.filter()

    for cv in cvs:

        def calculate_age(self):
            import datetime
            return int((datetime.datetime.now() - cv.birth_date).days / 365.25  )

        age = property(calculate_age)

    con = {

    'cvs': cvs,
    'age': age,
    }

    return render(request, 'base_cv.html', con)

And don't know why the fields after rendering and displaying, are empty.

Thanks for any help!


Solution

  • calculate_age should be a function on the model. You can use the @property decorator described here like:

    from datetime import datetime
    
    class Cv(models.Model):
    
        ...
    
        @property
        def age(self):
            return int((datetime.now().date() - self.birth_date).days / 365.25)
    

    Then your view can simply be:

    @login_required
    def base_cv(request):
        con = {'cvs': Cv.objects.all()}
        return render(request, 'base_cv.html', con)
    

    all is preferred over filter when you want all of the models.