1

I have a Django model (db is PostgreSQL) with a datetime field for publication date:

class Story(models.Model):
    ...
    pub_date = models.DateTimeField(default=datetime.datetime.now)
    ...

And a template tag to get those objects for a given month and year:

from django import template
from news.models import Story

class StoryYearListNode(template.Node):
    def __init__(self, varname):
        self.varname = varname

    def render(self, context):
        context[self.varname] = Story.live.dates("pub_date", "year").reverse()
        return ''

def do_get_story_year_list(parser, token):
    """
    Gets a list of years in which stories are published.

    Syntax::

        {% get_story_year_list as [varname] %}

    Example::

        {% get_story_year_list as year_list %}
    """
    bits = token.contents.split()
    if len(bits) != 3:
        raise template.TemplateSyntaxError, "'%s' tag takes two arguements" % bits[0]
    if bits[1] != "as":
        raise template.TemplateSyntaxError, "First arguement to '%s' tag must be 'as'" % bits[0]
    return StoryYearListNode(bits[2])

class StoryMonthListNode(template.Node):
    def __init__(self, varname):
        self.varname = varname

    def render(self, context):
        context[self.varname] = Story.live.dates("pub_date", "month").reverse()
        return ''

def do_get_story_month_list(parser, token):
    """
    Gets a list of months in which stories are published.

    Syntax::

        {% get_story_month_list as [varname] %}

    Example::

        {% get_story_month_list as month_list %}
    """
    bits = token.contents.split()
    if len(bits) != 3:
        raise template.TemplateSyntaxError, "'%s' tag takes two arguements" % bits[0]
    if bits[1] != "as":
    raise template.TemplateSyntaxError, "First arguement to '%s' tag must be 'as'" % bits[0]
    return StoryMonthListNode(bits[2])

register = template.Library()
register.tag('get_story_month_list', do_get_story_month_list)
register.tag('get_story_year_list', do_get_story_year_list)

But when I use the tag on a template, the date (using get_story_month_list as an example) is one month or year back from the pub date:

{% load date%}

        {% get_story_month_list as month_list %}
        <ul class="list-unstyled">
        {% for month in month_list %}
            <li><a href="{{ month|date:"Y/M"|lower }}/">{{ month|date:"F Y" }}</a></li>
        {% endfor %}
        </ul>

Any clue what I'm doing wrong?

Patrick Beeson
  • 1,667
  • 21
  • 36
  • I don't think it is related to your issue, but `DateTimeField` has built-in attribute to set it to "now", with `auto_now_add`. See this question: http://stackoverflow.com/questions/2771676/django-default-datetime-now-problem – Vincent Nov 20 '13 at 14:40
  • Yeah, I don't see that as related to my issue but I appreciate the information and link! – Patrick Beeson Nov 20 '13 at 15:51
  • So it looks as if this is a timezone issue (related: http://stackoverflow.com/questions/11174225/error-with-rendering-datetime-object-in-django-templates). I can solve the issue by turning off timezone in the settings, but ideally I'd like to leave this on. Any help here would be great. – Patrick Beeson Nov 20 '13 at 16:25
  • Have you read this page? https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#time-zone-aware-output-in-templates Plus, maybe your browser doesn't have the right timezone – Vincent Nov 20 '13 at 16:45
  • This code was implemented in Django 1.5. I upgraded to 1.6 and changed the function `Story.live.dates("pub_date", "month").reverse()` to `Story.live.datetimes("pub_date", "month").reverse()`. Instead of the timezone issue, I got a bug, which appears to be resolved for the next Django update (https://code.djangoproject.com/ticket/21432). – Patrick Beeson Nov 25 '13 at 15:42

1 Answers1

1

auto_now=True did'nt worked for me in django 1.4.1 but the below code saved me.

from django.utils.timezone import get_current_timezone
from datetime import datetime

class EntryVote(models.Model):
    voted_on = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        self.voted_on = datetime.now().replace(tzinfo=get_current_timezone())
        super(EntryVote, self).save(*args, **kwargs)
Ryu_hayabusa
  • 3,666
  • 2
  • 29
  • 32