Django Templates

Django Templates

Django's templating system allows developers to define how data should be presented. It��s a lightweight, Pythonic solution that nicely complements Django's model and view components. This tutorial will guide you through the basics of using Django templates.

1. Setting up the Environment:

Ensure you have Django installed. If not, you can do so with pip:

pip install django 

2. Create a new Django Project & App:

django-admin startproject myproject cd myproject python manage.py startapp myapp 

3. Setting up the Templates Directory:

Inside the myproject directory, create a new directory called templates. This is where we'll store our HTML templates.

4. Inform Django About the Templates Directory:

Edit myproject/settings.py:

TEMPLATES = [ { ... 'DIRS': [os.path.join(BASE_DIR, 'templates')], ... }, ] 

5. Basic Template:

Let��s create our first template. Inside the templates directory, create a new file named home.html:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Django App</title> </head> <body> <h1>Welcome to My Django App!</h1> </body> </html> 

6. Rendering the Template:

Let's create a view to render our template. In myapp/views.py:

from django.shortcuts import render def home(request): return render(request, 'home.html') 

Now, we need to create a URL mapping for this view. In myapp/urls.py:

from django.urls import path from .views import home urlpatterns = [ path('', home, name='home'), ] 

Ensure the app's URLs are included in the project's URL configuration (myproject/urls.py):

from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('myapp.urls')), ] 

7. Using Variables in Templates:

You can pass context data to templates. Let's say we want to pass a variable named username:

Edit myapp/views.py:

def home(request): context = { 'username': 'John Doe' } return render(request, 'home.html', context) 

In home.html, you can display the username using curly braces:

<h1>Welcome, {{ username }}!</h1> 

8. Using Template Tags and Filters:

Django templates come with built-in tags and filters.

For example, loops:

{% for item in items %} <li>{{ item }}</li> {% endfor %} 

Or filters to modify variables:

{{ name|lower }} <!-- This will convert the name to lowercase --> 

9. Template Inheritance:

You can create a base template and then extend it in other templates. This is useful for maintaining consistent layouts.

Create a base.html:

<html> <head> <title>{% block title %}Default Title{% endblock %}</title> </head> <body> {% block content %}{% endblock %} </body> </html> 

In home.html, extend the base:

{% extends "base.html" %} {% block title %}Home Page{% endblock %} {% block content %} <h1>Welcome, {{ username }}!</h1> {% endblock %} 

This is a very basic overview of Django templates. The system offers a lot more features, tags, filters, and functionalities. For a deeper dive, refer to the official Django documentation.


More Tags

configuration file-uri libcurl e-commerce puppeteer identity-column rest-assured video-processing counter aws-cloudformation

More Programming Guides

Other Guides

More Programming Examples