Code

Opened 3 years ago

Closed 13 months ago

#16443 closed Bug (fixed)

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

Reported by: BernhardEssl Owned by: nobody
Component: Generic views Version: master
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

Attachments (0)

Change History (4)

comment:1 Changed 3 years ago by BernhardEssl

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 3 years ago by BernhardEssl (next)

comment:2 Changed 3 years ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 2 years ago by aaugustin

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

Last edited 2 years ago by aaugustin (previous) (diff)

comment:4 Changed 13 months ago by aaugustin

  • Resolution set to fixed
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.