OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why is time in Django in 01.01.1900?

  • Thread starter Thread starter Coder4Fun250
  • Start date Start date
C

Coder4Fun250

Guest
I defined a model:

Code:
class Topic(models.Model):
    """A topic the user is learning about"""
    text = models.CharField(max_length=200)
    date_added = models.TimeField(auto_now_add=True)
def __str__(self):
        """Return a string representation of the model"""
        return self.text

I retrieve them in a view:

Code:
topics = Topic.objects.order_by("date-added")

I've been working on this project for a while now and the topics were displayed in the right order, but at some point I noticed that they're placed not in chronological order.

I tried to see the dates (in Django shell):

Code:
for topic in Topic.objects.order_by("date_added"):
   print(topic, topic.date_added.strftime("%d.%m.%Y $H:%M:%S"))

The result:

Code:
Hip-hop 01.01.1900 10:39:27
Rap2 01.01.1900 10:40:17
Chess 01.01.1900 15:45:32
Rock Climbing 01.01.1900 15:45:43
Programming 01.01.1900 15:51:18
Boxing 01.01.1900 18:21:59
Rap 01.01.1900 20:50:58

It shows, they're all added in the 1st Jan of 1900.

How to configure the date in Django so it displays it right?
<p>I defined a model:</p>
<pre><code>class Topic(models.Model):
"""A topic the user is learning about"""
text = models.CharField(max_length=200)
date_added = models.TimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model"""
return self.text
</code></pre>
<p>I retrieve them in a view:</p>
<pre><code>topics = Topic.objects.order_by("date-added")
</code></pre>
<p>I've been working on this project for a while now and the topics were displayed in the right order, but at some point I noticed that they're placed not in chronological order.</p>
<p>I tried to see the dates (in Django shell):</p>
<pre><code>for topic in Topic.objects.order_by("date_added"):
print(topic, topic.date_added.strftime("%d.%m.%Y $H:%M:%S"))
</code></pre>
<p>The result:</p>
<pre><code>Hip-hop 01.01.1900 10:39:27
Rap2 01.01.1900 10:40:17
Chess 01.01.1900 15:45:32
Rock Climbing 01.01.1900 15:45:43
Programming 01.01.1900 15:51:18
Boxing 01.01.1900 18:21:59
Rap 01.01.1900 20:50:58
</code></pre>
<p>It shows, they're all added in the 1st Jan of 1900.</p>
<p>How to configure the date in Django so it displays it right?</p>
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Ads by Eonads
Top