user6089877
user6089877

Reputation:

Django date is different in template than the database

I have a curious issue in my Django template.

I have model/table and for each entry I record the date/time for it. No problem, the date is correct in the database and in the Django admin pannel :

enter image description here

However, when I want to display the date/time in my template, this is different :

{{post.date|date:"d F H:m"}}

enter image description here

As you can see, the date displayed on the top right corner is 12:07. The most curious thing is for every post I made, the minutes are always "07". For example if I post at 15:30, my template will return 15:07.

Time zone in my settings.py :

# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'fr-fr'

TIME_ZONE = 'Europe/Paris'

USE_I18N = True

USE_L10N = True

USE_TZ = True 

My model :

class Statut(models.Model):
    text = models.CharField(max_length=100, null=False)
    image = models.FileField(upload_to=user_directory_path_articles, validators=[validate_file_extension], blank=True, null=True)
    author = models.ForeignKey(User, verbose_name="Auteur")
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de création")
    update = models.DateTimeField(auto_now=True, verbose_name="Dernière modification")

    def __str__(self):
        return self.text

My template :

(.......)
        <div class="w3-container w3-card-2 w3-white w3-round w3-margin"><br \>
          <img src="{{media}}{{post.author.profile.avatar}}" alt="Avatar" class="w3-left w3-circle-articles w3-margin-right" style="width:60px">
          <span class="w3-right w3-opacity">{{post.date|date:"d F H:m"}}</span>
          <h4>{{post.author}}</h4><br>
          <hr class="w3-clear">
          {%if post.image%}
            <p><img src="{{media}}{{post.image}}"></p>
          {%endif%}
          <p>{{post.text|safe}}</p>
          <div class="w3-row-padding" style="margin:0 -16px">
(.......)

Upvotes: 1

Views: 770

Answers (1)

Aniket Pawar
Aniket Pawar

Reputation: 2721

If you want to include year in your date format then you need to update your date format with "d F Y H:i",Also note that minutes is refer to i in django

{{post.date|date:"d F Y H:i"}}

for more date formats for django template have a look at the docs

Upvotes: 2

Related Questions