Formatting date in django template
Asked Answered
M

2

7

My template renders the tag {{ test.date}} in the following format -

2015-12-15T23:55:33.422679

When I try to format it using django's built in template tag date, it doesn't display anything.

Variations I've tried:

{{ test.date|date:"SHORT_DATE_FORMAT" }}

{{ test.date|date:"D d M Y" }}

models.py:

class Human(models.Model):
    name = models.CharField(max_length=50,default='',blank=False)


class Test(models.Model):
    human = models.ForeignKey(Human)
    date = models.DateTimeField(default=datetime.now)

views.py:

def list(request):
    h = Human.objects.all()
    s=[]
    for her in h: 
        t = h.test_set.all()
        s.extend(t)
    context = RequestContext(request, {'test_list': s,})
    return render_to_response('template.html', context)

I am then using it in template like this:

{% for test in test_list %}
     {{test.date}}
{% endfor %}

What am I missing?

Myall answered 17/12, 2015 at 10:21 Comment(5)
looks well, what you see without using |date : ? Can you add view in questionMellow
you should put your code in questionSublunary
What is test.date here? Is it an actual datetime object, or is it a string timestamp?Precursor
@DanielRoseman edited the code. Please check.Myall
@Baterson Edited.Please check now.Myall
M
1

I'm not sure what you want from this logic, but I think you can use this:

def list(request):
    test = Test.objects.all()
    return render(request, 'template.html', {'test':test})

and in template:

{% for t in test %}
    {% t.date %}
{% endfor %}

if you want display human, just add in cycle {% t.human.name %}

Mellow answered 17/12, 2015 at 14:20 Comment(4)
How is your code different? I'm doing the same thing. Passing a list of tests as test_list and then using test.dateMyall
@Myall It is more cleare and simple and it will work, if you created Test objectsMellow
But that is not my problem. My problem is that I can't format test.date using the date template tag. I want to know if the solution can be had using a queryset of test objectsMyall
What you see in test.date, without template tag date|?. In django 1.8 like i now, this template tage will work. Try my solution please, in my project it works. Here some tags for datetime docs.djangoproject.com/en/dev/ref/templates/builtins/#dateMellow
F
9

Answering an OLD post... but, the answer doesn't seem (to me) to be answering the original question - which was WHY isn't the Django inline template date formatting working...

The answer is (I believe) that the author was trying to output to his page something like:

"This is my date:{{test.date|date:"D d M Y"}}."

The problem, if this is truly what was being tried, is that the double quotes don't work in this situation. You need to do the following instead:

"This is my date:{{test.date|date:'D d M Y'}}."

Note the single quotes...

Forcier answered 4/12, 2020 at 16:14 Comment(1)
Django doc for the date filterSequestration
M
1

I'm not sure what you want from this logic, but I think you can use this:

def list(request):
    test = Test.objects.all()
    return render(request, 'template.html', {'test':test})

and in template:

{% for t in test %}
    {% t.date %}
{% endfor %}

if you want display human, just add in cycle {% t.human.name %}

Mellow answered 17/12, 2015 at 14:20 Comment(4)
How is your code different? I'm doing the same thing. Passing a list of tests as test_list and then using test.dateMyall
@Myall It is more cleare and simple and it will work, if you created Test objectsMellow
But that is not my problem. My problem is that I can't format test.date using the date template tag. I want to know if the solution can be had using a queryset of test objectsMyall
What you see in test.date, without template tag date|?. In django 1.8 like i now, this template tage will work. Try my solution please, in my project it works. Here some tags for datetime docs.djangoproject.com/en/dev/ref/templates/builtins/#dateMellow

© 2022 - 2024 — McMap. All rights reserved.