InfinteScroll
InfinteScroll

Reputation: 684

Filtering a DateField Object Django

I've read a whole bunch of similar Stackoverflows on this issue, however my issue seems to be more specific. I can't get the date to display in anyway but the default.

Here is the model in question:

class Blog (models.Model):
    ...
    posted = models.DateField(db_index=True)

It's then passed to the template like so, as part of the objects included in Blog.objects.all():

def index(request):
    return render_to_response('index.html', {
        'categories': Category.objects.all(),
        'posts': Blog.objects.all()[:10:-1],
    })

So currently in my templates, I can display the DateField info by writing the following in a template, as posted is a property of the object :

{% for post in posts %}
    {{ post.posted }}
{% endfor %}

And if I do it that way it displays the date in a rather ugly: 2014-02-24

How can I change this display?

Things that I have already tried:

The documentation for 1.6 makes it sound really simple with the "|date" filter, but its just not working on my end. Any ideas?

Thank you.

Upvotes: 0

Views: 215

Answers (1)

Mathieu Dhondt
Mathieu Dhondt

Reputation: 8914

I'm not able to test this right now, but something in the docs caught my eye.

The documentation for the date template filter says

if value is a datetime object (e.g., the result of datetime.datetime.now()), the output will be the string 'Wed 09 Jan 2008'.

Whereas the documentation for the DateField model field states that the field contains

a date, represented in Python by a datetime.date instance.

Could it be that the date template filter doesn't take a datetime.date instance as its argument? Try and change the Blog model's posted field to a DateTimeField and see what happens.

Upvotes: 3

Related Questions