Views.py & template (queryset)

48 views
Skip to first unread message

Franck

unread,
Jun 1, 2016, 12:40:56 PM6/1/16
to Django users
Hello,

First Django project... sorry ;-)

I can run that in manage.py shell, result is ok.
     Test.objects.filter(dateregister__contains='2016').count()

But how I can show this result in the template ?
{{ test.count }} works directly in the template without modify views.py, I tried with
{{ test.filter(dateregister__contains='2016').count() }}   but no result...


Models.py

from __future__ import unicode_literals
from django.db import models
from django.utils import timezone

class Test(models.Model):
    name
= models.CharField(max_length=10, editable=False)
    dateregister
= models.DateTimeField(auto_now=False, auto_now_add=False, null=True, blank=True, editable=False)
   
def __str__(self):              # __unicode__ on Python 2
       
return self.name


Views.py
from .models import Test
from datetime import datetime
from django.shortcuts import render, get_object_or_404

def test(request):
    test
= Test.objects.order_by('name')
   
return render(request, 'test/test.html', {'test': test})

Thanks for your help !

Stephen J. Butler

unread,
Jun 1, 2016, 1:00:41 PM6/1/16
to django...@googlegroups.com
From the template language reference:


"""
Because Django intentionally limits the amount of logic processing available in the template language, it is not possible to pass arguments to method calls accessed from within templates. Data should be calculated in views, then passed to templates for display.
"""

If you want to do something like this then you're better switching to a more complete template language, like Jinja2. But I'd also suggest you prepare your querysets entirely before passing them to templates.

Also, for Date/DateTime/Time fields you should use the lookups provided. Do this instead: .filter(dateregister__year=2016)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/03de09e4-1a70-4bf3-a669-dce6a22f24d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Franck

unread,
Jun 2, 2016, 7:36:06 AM6/2/16
to Django users
Hello Stephen,
Thanks for your help, ok I think it's better to used views for what I need to do.
Last question, how I can define this in view and template ?
For the moment I used 'for' & 'endfor' in my template to show all my items, but I want to show "date2016" in only one field.

Views.py
from .models import Test
from datetime import datetime
from django.shortcuts import render, get_object_or_404

def test(request):
    test
= Test.objects.order_by('name')

   
date2016 = Test.objects.filter(dateregister__contains='2016').count()    ??????

   
return render(request, 'test/test.html', {'test': test})

Template:

{% for test in test %}
<td>{{ test.name }}</td>
<td>{{ test.dateregister }}</td>
{% endfor %}

{% test.date2016 %}   ????

Thanks for your help !

Stephen J. Butler

unread,
Jun 2, 2016, 10:44:23 AM6/2/16
to django...@googlegroups.com
Add date2016 to your context:

{ 'test': test, 'date2016': date2016 }

Then it's just {{ date2016 }} in your template.

Reply all
Reply to author
Forward
0 new messages