Django variables templates

Django variables templates

In Django, templates are used to generate HTML dynamically. One of the primary features of templates is their ability to receive and work with context data, often referred to as template variables. In this tutorial, we'll explore how to pass and utilize variables within Django templates.

1. Setting Up a Django Project:

If you haven't already set up a Django project, do so with the following commands:

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

2. Creating a Model:

Let's assume you have a simple model in myapp/models.py:

from django.db import models class Person(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() email = models.EmailField() def __str__(self): return self.name 

Run migrations to apply this model:

python manage.py makemigrations python manage.py migrate 

3. Passing Variables to Templates:

In your myapp/views.py, let's create a view that passes a Person instance to a template:

from django.shortcuts import render from .models import Person def person_detail(request): person = Person.objects.first() # Get the first person in the database for simplicity context = { 'person': person } return render(request, 'myapp/person_detail.html', context) 

Here, we are sending a person variable to our template.

4. Accessing Variables in Templates:

Now, let's see how to access the passed variables in the template. Create a new template myapp/templates/myapp/person_detail.html:

<h1>{{ person.name }}</h1> <p>Age: {{ person.age }}</p> <p>Email: {{ person.email }}</p> 

In the template, we're using the double curly braces {{ }} to access and render the context variables.

5. Using Variables with Filters:

Django provides several built-in template filters to manipulate variables. For instance, you can change the case of a string, perform date formatting, and more.

Here's how you can use the lower filter to convert the person's name to lowercase:

<h1>{{ person.name|lower }}</h1> 

6. Looping Over Variables:

If the context variable is a list or a queryset, you can loop over it in the template using the {% for %} tag.

Assuming you pass all people as a variable:

def all_people(request): people = Person.objects.all() context = { 'people': people } return render(request, 'myapp/all_people.html', context) 

You can loop over this in the template myapp/templates/myapp/all_people.html:

<ul> {% for person in people %} <li>{{ person.name }} - {{ person.age }} years old</li> {% endfor %} </ul> 

7. Checking Variable Existence:

In templates, you can also check if a variable exists or has a value:

{% if person %} <h1>{{ person.name }}</h1> {% else %} <p>No person found.</p> {% endif %} 

Conclusion:

This tutorial gives a brief overview of how to pass and utilize variables in Django templates. The template system in Django is quite powerful, with many tags and filters available to manipulate data and control the rendered output.


More Tags

pyc load-data-infile eslint-config-airbnb bmp six scikit-learn pattern-matching mv aspect-ratio pg-restore

More Programming Guides

Other Guides

More Programming Examples