Django output html time is 8 hours ahead of database time
Question: I'm very new to **Django** and I've been learning the framework from the book
_"Practical django Projects"_ (the book teaches us to write a cms). My code
runs fine, but I have time problem with the `get_absolute_url` function below.
It's actually outputting the link 8 hours ahead of the time saved in my
database. I used python shell to look at the saved time in the database and
the time saved in the admin interface, they are all correct. But when I use
the `get_absolute_url func` below to generate the link in browser, it becomes
8 hours ahead and throws the day off. I set the correct zone in my Django
setting file. I cannot figure out what's wrong.

How I can fix this (I'm using sqlite3 for my database, Django 1.4.1)?

Here is my code for the Entry class:

    
    
    import datetime
    
    from django.db import models
    from django.contrib.auth.models import User
    
    from tagging.fields import TagField
    from markdown import markdown
    from django.conf import settings
    from django.utils.encoding import smart_str
    
    class Entry(models.Model):
    
    live = LiveEntryManager()
    objects = models.Manager()
    #define constant options
    LIVE_STATUS = 1
    DRAFT_STATUS = 2
    HIDDEN_STATUS = 3
    STATUS_CHOICES = (
        (LIVE_STATUS, 'Live'),
        (DRAFT_STATUS,'Draft'),
        (HIDDEN_STATUS, 'Hidden'),
        )
    #adding features to admin interface
    class Meta:
        ordering = ['-pub_date']
        verbose_name_plural = "Entries"    
    #define model fields:
    title = models.CharField(max_length=250)
    excerpt = models.TextField(blank=True) #It's ok to not add anything for this field
    body = models.TextField()
    pub_date = models.DateTimeField(default=datetime.datetime.now()) 
    slug = models.SlugField(unique_for_date='pub_date')
    
    enable_comments = models.BooleanField(default=True)
    featured = models.BooleanField(default=False)
    status = models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS)
    #HTML
    excerpt_html = models.TextField(editable=False, blank=True)
    body_html = models.TextField(editable=False, blank=True)
    #third party:
    tag = TagField()
    #relationship fields:
    categories = models.ManyToManyField(Category)
    author = models.ForeignKey(User)
    #define methods:
    def save(self, force_insert=False, force_update=False):#modify the model.save() method
        self.body_html = markdown(self.body)
        if self.excerpt:
            self.excerpt_html = markdown(self.excerpt)#from excerpt field to excerpt_html
        super(Entry, self).save(force_insert, force_update)
    def get_absolute_url(self):
        return "%s" % self.pub_date.strftime("year:%Y/day:%d/hour:%H/minute:%M/second:%S")
    #@models.permalink
    #def get_absolute_url(self):
        #return ('coltrane_entry_detail', (), {'year': self.pub_date.strftime("%Y"),
                                              'month': self.pub_date.strftime("%b").lower(),
                                              'day': self.pub_date.strftime("%d"),
                                              'slug': self.slug})
    def __unicode__(self):
        return self.title
    

This is my entry_archive.html:

    
    
    {% extends "base_entries.html"%}
    
    {%block title%}{{block.super}} | Latest entries{% endblock %}
    
    
    {% block content %}
    {% for entry in latest %} 
    
    <h2>{{entry.title}}</h2>
    <p>Published on {{ entry.pub_date|date:"F j P s, Y" }}</p>
    {% if entry.excerpt_html%}
        {{entry.excerpt_html|safe}}
    {% else %}
        {{entry.body_html|truncatewords_html:"50"|safe}}
    {% endif%}
    <p><a href="{{entry.get_absolute_url}}">Read full entry ...</a></p>
    
    {% endfor %}
    {%endblock%}
    
    {%block whatis%}
     <p>This is a list of the latest {{latest.count}} entries published in my blog.</p>
    
    {% endblock %}
    

I couldn't post screenshot because i'm a new user. {{ entry.pub_date|date:"F j
P s, Y" }} in my html give me correct time: December 28 11:24 a.m. 45, 2012.
But {{entry.get_absolute_url}} gives me
year:2012/day:28/hour:19/minute:24/seconds:45

I add the () to pub_date =
models.DateTimeField(default=datetime.datetime.now()) as you guys suggested,
but the result is still the same(the book actually suggests not to add () ).
The thing troubles me is that {{ entry.pub_date|date:"F j P s, Y" }} gives me
the correct time on my html, but {{entry.get_absolute_url}} is 8 hours ahead.
I set my setting.py time zone to TIME_ZONE = 'America/Los_Angeles'. Thanks for
all the quick response, but this is killing me...


Answer: change the following:

    
    
    pub_date = models.DateTimeField(default=datetime.datetime.now)
    

to Either

    
    
    pub_date = models.DateTimeField(default=datetime.datetime.now())
    

or

    
    
    pub_date = models.DateTimeField(auto_now_add=True)
    

