28👍
count
requires an argument. It returns the number of instances of a particular item in a list.
>>> l = range(10) + range(10)
>>> l.count(5)
2
2
here is the number of 5
s in the list. If you want the length of a list, use len
.
>>> len(l)
20
11👍
count()
finds the number of times an item occurs in a list and hence needs that item as an argument. I think your looking for the number of items in a list. For this use len()
not count()
flashcard_dict['words_count'] = len(flashcard_list)
flashcard_dict['words_known'] = len(flashcard_list.filter(known=Yes))
An example of count()
would be
flashcard_dict['dog_count'] =flashcard_list.count('dog')
- [Django]-Filtering by custom date range in Django admin
- [Django]-Docker-compose with django could not translate host name "db" to address: Name or service not known
- [Django]-Get request data in Django form
7👍
It’s not really clear what you are trying to do here. flashcard_list
is an (empty) list, which you define before your loop. It doesn’t make sense to call Django queryset functions like count
and filter
on it (there is a list method named count, but as the error says, it takes an argument, and counts the number of times that argument is found in the list).
Did you mean to use flashcard.count()
instead? That still doesn’t make sense, because flashcard is a single Flashcard instance, not a queryset. You’ll need to explain a bit more exactly what you’re hoping to do.
Edit after comment Right, so I think the issue is that you’re trying to do all this inside a loop that iterates through each Flashcard, for some reason. In fact, I don’t think you want the loop at all. Something like this would be better:
def report(request):
flashcard_dict = {}
flashcards = Flashcard.objects.all():
flashcard_dict['list_object'] = flashcards
flashcard_dict['words_count'] = flashcards.count()
flashcard_dict['words_known'] = flashcards.filter(known=True).count()
flashcard_dict['percent_known'] = int(float(flashcard_dict['words_known']) / flashcard_dict['words_count'] * 100)
return render_to_response('report.html', flashcard_dict)
Here you can see that you’re operating on the queryset of all flashcards, rather than an empty list. And you’re building up a single dictionary, not a list of dictionaries, and that dictionary itself becomes the template context – so in the template you can reference {{ words_count }}
etc.
- [Django]-Pulling data to the template from an external database with django
- [Django]-Django content types – how to get model class of content type to create a instance?
- [Django]-Django CSRF framework cannot be disabled and is breaking my site
3👍
The count
method of a list counts the amount of times that x
appears on the list. Check out the doc here.
If you want to know the amount of items in the list you need to use len()
>>> a = ['a', 'b', 'c']
>>> print len(a)
3
- [Django]-How can I turn Django Model objects into a dictionary and still have their foreign keys?
- [Django]-Error : "You are trying to add a non-nullable field"
- [Django]-How to create SaaS application with Python and Django
2👍
The count
method is not quite what you’re looking for. On Python lists, list.count(x)
tells you how many occurrences of x
are in list
. You want len(flashcard_list)
. The function len
is a built-in function that will tell you the length of a number of Python object types.
- [Django]-Django: Where to put helper functions?
- [Django]-Django: How to override unique_together error message?
- [Django]-How to make email field unique in model User from contrib.auth in Django
0👍
You need to give some argument to count(x).
It returns the number of times x appears in the list.
>>> mylist = [1,2,3,2]
>>> mylist.count(2)
2
>>> mylist.count()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: count() takes exactly one argument (0 given)
>>>
It is difficult to understand what you are trying to achieve your flashcard_list
list is empty. So before calling count
on it you need to add something to the list.
- [Django]-Get user information in django templates
- [Django]-How to auto insert the current user when creating an object in django admin?
- [Django]-Sending images using Http Post
0👍
alist = []
alist.append('a')
alist.count('a')
1
This would count all the ‘a’ in your list.
- [Django]-ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
- [Django]-How to get the app a Django model is from?
- [Django]-RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS