Opened 13 years ago

Closed 11 years ago

#16443 closed Bug (fixed)

previous_day from views.generic.dates.DayArchiveView doesn't return the previous day

Reported by: Bernhard Essl Owned by: nobody
Component: Generic views Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The previous_day from views.generic.dates.DayArchiveView returns not the previous day instead the day before the previous day (24 June -> 22 June).

# blog/models.py
from django.db import models

class Post(models.Model):
    title = models.TextField()
    pub_date = models.DateTimeField()

    def __unicode__(self):
        return u"%s" % (self.pub_date,)

# url.py
from django.conf.urls.defaults import patterns, include, url
from django.views.generic import dates
from django.views import generic
from blog.models import Post
    
class PostDayArchive(generic.DayArchiveView):
    queryset = Post.objects.all()
    date_field = 'pub_date'

urlpatterns = patterns('',
    url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/$', PostDayArchive.as_view()),
)

# templates/blog/post_archive_day.html
{{ previous_day }}

# 4 objects post from june 21-24
Post.objects.all()
[<Post: 2011-06-24 12:33:10>, <Post: 2011-06-23 13:33:10>, <Post: 2011-06-22 13:33:10>, <Post: 2011-06-21 13:33:10>]

# page request
$ curl http://127.0.0.1:8000/2011/jun/24/
June 22, 2011

When I open the page with the URL /2011/jun/24/ I get "June 22, 2011" for the "previous_day"; on /2011/jun/23/ I get "June 21, 2011" and on /2011/jun/22/ I get None.

Some notes:

  • On production sometimes previous_day is working correctly. Didn't find a pattern. :(
  • I use Django trunk (16527)
  • The code works fine with models.DateField() instead of models.DateTimeField()
  • I tested with MySQL and SQLite
  • all other template tags like next_day or today are working

Change History (4)

comment:1 by Bernhard Essl, 13 years ago

I traced down the problem a bit https://code.djangoproject.com/browser/django/trunk/django/views/generic/dates.py#L560.

For the date 2011-06-24; naive_result (a datetime value of the previos day) is 2011-06-23 and the raw SQL query looks like this:

.. WHERE `blog_post`.`pub_date` <= '2011-06-23 00:00:00' ORDER BY `blog_post`.`pub_date` DESC LIMIT 1;

I think it would be better to ask for <= '2011-06-24 00:00' in this case to return all records from the previous day. And sure it's not a problem with DateFields.

Version 0, edited 13 years ago by Bernhard Essl (next)

comment:2 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Aymeric Augustin, 12 years ago

This problem is probably fixed since this commit, where the implementation of date filters was largely rewritten.

Last edited 12 years ago by Aymeric Augustin (previous) (diff)

comment:4 by Aymeric Augustin, 11 years ago

Resolution: fixed
Status: newclosed

Please re-open if you can reproduce the issue with Django 1.5

Note: See TracTickets for help on using tickets.
Back to Top