Django is a popular Python web framework designed to help developers take applications from concept to completion as quickly as possible. Django templates is a powerful feature that aids in this, as it allows developers to dynamically generate HTML web pages by substituting data into pre-formatted templates.
In order to use Django templates, the first thing you would need to do is create a template. Templates in Django are like text files that allows both static and dynamic HTML content. They are mostly HTML files but can also produce XML, CSV, or other text-based formats (Django Software Foundation official documentation).
For example, you could create a basic HTML template as such:
```
{{ welcome_message }}
In the above, the Django template language is used within {{}} to substitute `welcome_message` with some Python value. This is an instance of variable substitution in Django templates (Real Python Tutorial).
The second key step is to create a view that uses this template. According to Django’s official documentation, a view in Django is like a type of Web page in your Django application that generally serves a specific function and has a specific template. For instance, a view might be “blog archive” page of your site, displaying a list of post excerpts. You could create a view as follows:
```
from django.shortcuts import render
def welcome(request):
context = { ‘welcome_message’: ‘Hello, world!’ }
return render(request, ‘welcome.html’, context)
```
In the above, the `welcome.html` template you created earlier is referred to, and a Python dictionary `context` is passed to it.
Finally, you will have to map a URL to this view using Django’s URL dispatcher. Thereby, when that URL is called from a web browser, Django will execute the associated view, which in turn renders the template with the provided context and returns as a HTTP response to the browser (Tutorial: The Local Library website, MDN Web Docs).
This is a basic example of how to use Django templates but there’s much more to the Django template language, such as filters, tags, and inheriting from “base” templates. For further reading on these and other advanced topics, consult Django’s official templates API documentation and How to use templates in Django – Python Django tutorial series by CodeWall.
Sources:
1. Django Software Foundation official documentation: https://docs.djangoproject.com/
2. How to use templates in Django – Python Django tutorial series, CodeWall: https://www.codewall.co.uk/how-to-use-templates-in-django-python-django-tutorial-series/
3. Django Tutorial: The Local Library website, MDN Web Docs: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Tutorial_local_library\_website
4. Django Templates, Real Python: https://realpython.com/django-templates/