Django template tag: simplify data display and improve code reusability
In Django development, templates are used to dynamically render data into HTML pages. This article will introduce how to use Django template tags to simplify data display logic and avoid duplicating code in views.
Suppose you have a simple course list HTML template:
The corresponding view code is as follows:
view passes the course data to the template, which is ultimately displayed on the web page as follows:
Now, let’s say you need to display the total number of courses on a web page. One way is to add calculation logic in the view:
def course_list(request): total_courses = Course.objects.count() return render(request, 'courses.html', {'courses': courses, 'total_courses': total_courses})
But if your website has multiple pages (such as blog page, author page, instructor page) that all need to display the total number of courses, you need to repeat this logic in each view, which will lead to redundant code and difficult to maintain . At this time, Django template tags come in handy.
To put it simply, Django template tags are special tags that allow you to add custom functionality to the Django template system. It can improve code reusability and avoid writing the same logic repeatedly in views.
Suppose your course app needs to display the following data:
Instead of adding calculation logic in every view, use template tags to simplify things.
templatetags
folder under your courses app: Your folder structure is as follows:
<code> courses/ ├── templatetags/ ├── __init__.py └── course_tags.py</code>
course_tags.py
file, define the template tags that calculate the total number of courses, the number of available courses, and the number of registered students: from django import template from courses.models import Course register = template.Library() @register.simple_tag def total_courses(): return Course.objects.count() @register.simple_tag def available_courses(): return Course.objects.filter(is_available=True).count() @register.simple_tag def enrolled_students(course_id): course = Course.objects.get(id=course_id) return course.enrolled_students.count()
Now you can load and use these custom tags in your HTML template to display relevant data.
In the course_list.html
template, load the custom tags and use them to display the total number of courses and the number of available courses:
{% load course_tags %} <h1>所有课程</h1> <p>总课程数:{% total_courses %}</p> <p>可用课程数:{% available_courses %}</p> {% for course in courses %} - {{ course.name }} - {{ course.description }} {% endfor %}
This template will display:
On the course details page, you can use the enrolled_students
template tag to display the number of registered students for a specific course:
def course_list(request): total_courses = Course.objects.count() return render(request, 'courses.html', {'courses': courses, 'total_courses': total_courses})
enrolled_students
tag receives course.id
as a parameter and returns the number of registered students for the course.
This article demonstrates through examples how to use template tags in Django to avoid duplicating the logic of adding courses and student counts in views. Template tags improve code reusability and maintainability.
Contact - @syedamahamfahim ?
The above is the detailed content of How to Create Custom Template Tags in Django?. For more information, please follow other related articles on the PHP Chinese website!