Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
Django's template system allows you to separate presentation logic from the application logic, making your code more maintainable and reusable. This tutorial will guide you through the basics of Django's template system.
python manage.py startapp myapp
Replace myapp
with your desired app name.
Create a templates directory: By default, Django looks for templates in a folder named templates
within each app. Create a new folder named templates
inside the myapp
folder.
Create a template: Inside the templates
folder, create a new file named index.html
. This file will serve as a simple template for this tutorial. Add the following HTML code to the file:
<!DOCTYPE html> <html> <head> <title>My Django App</title> </head> <body> <h1>Welcome to My Django App!</h1> <p>{{ message }}</p> </body> </html>
In Django templates, you can use double curly braces {{ }}
to insert the value of a variable. In this case, we're inserting the value of the message
variable.
myapp/views.py
file and create a new view function that will render the index.html
template:from django.shortcuts import render def index(request): context = {'message': 'Hello, Django Template System!'} return render(request, 'index.html', context)
The render()
function takes the request object, the template name, and an optional dictionary called context
. The context
dictionary contains variables that will be passed to the template.
myapp
folder, create a new file named urls.py
and add the following code:from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
Then, include your app's URLs in the project's urls.py
file:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ]
This will make the index
view accessible at the /myapp/
path.
python manage.py runserver
command, and navigate to http://127.0.0.1:8000/myapp/
in your web browser. You should see the rendered index.html
template, with the message "Hello, Django Template System!" displayed.With the basic template system set up, you can start exploring more advanced features like template inheritance, filters, and custom template tags. The Django template system provides a powerful and flexible way to manage your application's presentation logic.
Django Template Tags and Filters:
{% if user.is_authenticated %} <p>Welcome, {{ user.username }}!</p> {% else %} <p>Please log in</p> {% endif %}
Getting Started with Django Templates:
<!DOCTYPE html> <html> <head> <title>{{ title }}</title> </head> <body> <h1>Hello, {{ user.username }}!</h1> </body> </html>
Django Template Inheritance Explained:
Description: Template inheritance allows creating a base template with common structure and extending it in other templates.
Code: Example of a base template and an extended template:
<!-- base_template.html --> <!DOCTYPE html> <html> <head> <title>{% block title %}Default Title{% endblock %}</title> </head> <body> <div id="content"> {% block content %}{% endblock %} </div> </body> </html>
<!-- extended_template.html --> {% extends 'base_template.html' %} {% block title %}Extended Title{% endblock %} {% block content %} <h1>Hello, {{ user.username }}!</h1> {% endblock %}
Django Template Context Variables:
# views.py from django.shortcuts import render def my_view(request): context = {'user': request.user, 'title': 'My Page'} return render(request, 'my_template.html', context)
Conditional Statements in Django Templates:
{% if user.is_authenticated %} <p>Welcome, {{ user.username }}!</p> {% else %} <p>Please log in</p> {% endif %}
Iterating Over Lists in Django Templates:
{% for %}
loop to iterate over lists or querysets in Django templates.<ul> {% for item in my_list %} <li>{{ item }}</li> {% endfor %} </ul>
Including Templates in Django:
Description: Use the {% include %}
tag to include other templates within a template.
Code: Example of including a template in another template:
<!-- included_template.html --> <p>This is an included template</p>
<!-- main_template.html --> <h1>Main Template</h1> {% include 'included_template.html' %}
Django Template Built-In Filters:
<p>{{ some_variable|default:"No value" }}</p>
Django Template Custom Filters:
Description: Define custom template filters to extend the functionality of Django templates.
Code: Example of creating a custom filter:
# custom_filters.py from django import template register = template.Library() @register.filter(name='double') def double(value): return value * 2
<!-- template.html --> <p>{{ some_variable|double }}</p>
Django Template Variables and Expressions:
<p>Total: ${{ price * quantity }}</p>
Django Template for Loops and If Conditions:
for
loops and if
conditions in Django templates for complex rendering logic.for
and if
in a Django template:{% for item in my_list %} {% if item.is_available %} <p>{{ item.name }} is available</p> {% endif %} {% endfor %}
Dynamic Content in Django Templates:
<h1>{{ page_title }}</h1> <p>{{ page_content }}</p>