Re: Views.py & queryset

2016-06-02 Thread James Schneider
On Wed, Jun 1, 2016 at 9:44 AM, Franck  wrote:

> Hello,
>
> Sorry first project...
>
> I can run :
> *Test.objects.filter(dateregister__contains='2016').count()in
> manage.py shell, result is ok.*In template, *{{ test.count }} *works
> well, but I tried *{{ *test.filter(dateregister__contains='2016').count()*
> }} *without success.
>
> *I can't use queryset directly in the template ?  (need to modify views.py
> ?)*
>

In most cases, you can't (and don't want to) use a queryset directly in the
template. That type of logic usually ends up in the view. The view is
responsible for preparing the data, whereas the template is responsible for
formatting the data provided by the view.

The reason that {{ test.count }} works is because you are storing a
queryset in the context, rather than the actual object (or list of objects
in this case). There are perfectly valid use cases for doing so, but keep
in mind there is a difference between a queryset and an actual object or
list of objects. If you use a queryset, you'll incur a database hit every
time you refer to that variable in the template, which is likely not what
you want. A notable exception is when looping over the queryset.

I'd recommend reading up on the template language and becoming familiar
with the (deliberate) restrictions on the template language:
https://docs.djangoproject.com/en/1.9/ref/templates/language/#variables

-James

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CA%2Be%2BciUid88r7be8sc2eGpNfcK7G-TWeDyhf3Fw7gE84%3DhsBCg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Views.py & queryset

2016-06-01 Thread Franck
Hello,

Sorry first project...

I can run :  
*Test.objects.filter(dateregister__contains='2016').count()in manage.py 
shell, result is ok.*In template, *{{ test.count }} *works well, but I 
tried *{{ *test.filter(dateregister__contains='2016').count()* }} *without 
success.

*I can't use queryset directly in the template ?  (need to modify views.py 
?)*
*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.db.models import Q, F, count
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 !!*

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/74dd256d-8fde-45ed-b619-4a400f3f1f25%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.