In this Python tutorial, I will explain how to check the length of a string or a list in a Django application in Python.
- Check length of a string in Django
- Length of string in Django template
- Django template length of list
- Django template if length greater than
- Get length of queryset Django
Check length of a string in Django
To simply check the length of a string in Django, you can use the len() function. This function takes a string input and returns an integer value i.e. the length of a string.
You can use the len function as shown below in the below example. I am executing the code in the Django shell.
To start using the Django shell, run the below command in your windows command prompt:
django-admin shell
Now you can see how the len() function works and returns the length of a string:
>>> username = 'abc'
>>> print(len(username))
3
In this way, you can find the length of a string. However, if you want to find the length of a string inside a Django template, you can use the length filter.
Read How to Get Current time in Django
Length of string in Django template
Suppose in a case, you are generating a dynamic string and passing it into a Django template and you need to find the length of the string. In that case, you can use the length filter to find the length.
For example, I have created a view and passed a string from this view to a template. My views.py file looks like this:
from django.shortcuts import render
def index(request):
parameters={
'name' : 'Harris'
}
return render(request, 'index.html', parameters)
If I want to render the length of the string in the “Harris” variable, I can use the length function as shown below in the template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
</head>
<body>
<h3>Name is:{{name}}</h3>
<h3>Length of name is:{{name|length}}</h3>
</body>
</html>
In the above template, I accessed the name variable and applied the length filer on it.
In this way, you can find the length of a string in a Django template.
Read How to Create model in Django
Django template length of list
Generally, we need to find the length of a list in a Django template while we try to pass a list inside a dictionary from a view to a template. In that case, we can use the length filter to find the length of a list.
For example, I have created a view that is passing a dictionary containing a list of elements. The views.py file looks like this:
from django.shortcuts import render
def index(request):
list1=['Steve', 'Peter', 'Jonathan']
return render(request, 'index.html', {'list1':list1})
You can see that I have passed a list through a dictionary to the template. Now in the template file, you can use the length filter to find the length of this list as:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
</head>
<body>
<h3>Length of list is :{{list1|length}}</h3>
</body>
</html>
In this way, you can find the length of a list using the length filter in Python Django.
Also read, Difference between app and project in Django
Django template if length greater than
As you have learned about the length filter, let us see an example where we will compare the length of a string using the greater than operator(>) in Django template.
In the below example, I have used the an if statement and a greater than operator to compare the length of a list specified in a view. The source code of view file is:
from django.shortcuts import render
def index(request):
list1=['Steve', 'Peter', 'Jonathan', 'Rambo', 'Vicky', 'Daisy']
return render(request, 'index.html', {'list1':list1})
I have passed this list containing six items to a template file which is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
</head>
<body>
{% if list1|length > 5%}
<h3>Size of list is too large</h3>
<h4>Size is : {{list1|length}}</h4>
{% endif %}
</body>
</html>
I have used an if block. The code inside this if block will only execute if the length of the list is greater than 5, which is True in our case. Let’s see the output now.
Thus, you might have learned how you can find the length of a list in a Django template.
Read Python Django get admin password
Get length of queryset Django
Sometimes, you are fetching a resultset from a database and rendering the records in the HTTP response. You may want to get the length of the resultset i.e. the number of records in the resultset and show the results accordingly.
In such a case, you can use the count() method or the len() function to find the number of records in a queryset.
For example, I have created a view that fetches a queryset from a table named Employees. The table contains the records as shown below:
id | first_name | last_name | department |
---|---|---|---|
1601 | Chris | Hemsworth | Research |
1602 | Helena | Williams | Finance |
1603 | Jerry | James | Marketing |
1604 | Dwayne | Morkel | Finance |
My views.py file looks like this:
from app1.models import Employees
from django.shortcuts import render
def index(request):
querysetlen = Employees.objects.count()
return render(request, 'index.html', {'queryset':querysetlen})
You can see that how I have used the count() function to find the length of the queryset.
Now I am rendering this count into a Django template named index.html which is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
</head>
<body>
<h3>Number of records is:{{queryset}}</h3>
</body>
</html>
You can also use the len() function. Just make the below changes to your views.py file:
from app1.models import Employees
from django.shortcuts import render
def index(request):
querysetlen = len(Employees.objects.all())
return render(request, 'index.html', {'queryset':querysetlen})
In this way, you can find the length of a queryset in Django.
You may like the following Python Django tutorials:
- Django for loop
- Python Django group by
- Python Django get
- Python Django filter
- How to setup Django project
- Union operation on models Django
Thus, you might have learned how to find the length of various Django objects using various techniques.
- Check length of a string in Django
- Length of string in Django template
- Django template length of list
- Django template if length greater than
- Get length of queryset Django
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.