Friday, December 28, 2012

Re: Django output time is 8 hours ahead of database time

Could you please provide timezone from your local system ? Pacific
time ? or UTC ?
Many thanks,

Serge


+380 636150445
skype: skhohlov


2012/12/29 Jeff Hsu <jyhsu1125@gmail.com>:
> 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.
>
> 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 %}
>
> {{ 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
>
> 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 think
> self.pub_date.strftime("year:%Y/day:%d/hour:%H/minute:%M/second:%S") convert
> the pub_date into UTC, but I don't know why. I set my setting.py time zone
> to TIME_ZONE = 'America/Los_Angeles'. How I can fix this (I'm using sqlite3
> for my database, Django 1.4.1)?
>
> Thanks a lot,
>
> Jeff
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/fLfdw5gUfaMJ.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment