Sulav Jung Hamal - Blog - 2023/05/02 -
Django is a popular Python web framework that provides many features for building scalable web applications. One of its key features is its built-in templating engine, which allows developers to separate the application's logic from its presentation layer. This article will explain how to use templates in a Django project, including how to create and render templates.
A template in Django is a file that contains the HTML markup and other presentation logic that is used to generate the final output that is sent to the user's browser. Templates are used to separate the application's logic from its presentation layer, making it easier to maintain and update the application's user interface.
Create a templates
directory in each of your application's root directory. If it already exists, skip to the next step.
##create the templates directory
mkdir templates
//Your File structure.
root directory
myapp/
templates/
Once you've created a template folder inside each app, you'll need to tell Django where to find these templates. To do this, you'll need to add the relative path to the template directory to the settings.py
file for your Django project. Here's how:
settings.py
file for your Django project.TEMPLATES
section. This is where you'll configure the settings for your project's templates.DIRS
option within the TEMPLATES
section. This option is a list of directories where Django looks for templates.DIRS
list like this:TEMPLATES = [
{
# other options here...
'DIRS': [
'appname/templates',
],
},
]
appname/templates
direrctory when rerndering views that belong to the app
with appname
here.Once you've created the template folder and configured the settings for your Django project, you're ready to create templates that can be used to render web pages. In this section, we'll look at how to create a simple template file and how to use context to pass data from a view to a template.
To create a template file, you'll need to create a new HTML file in your app's template directory. For example, if your app is named myapp
, you would create a file named my_template.html
in the myapp/templates
directory.
Here's an example of a simple template file that uses context to display a message:
<!DOCTYPE html>
<html>
<head>
<title>{{ page_title }}</title>
</head>
<body>
<h1>Hello, {{ name }}!</h1>
<p>{{ message }}</p>
</body>
</html>
In this example, we've created a basic HTML file that includes three placeholders for data that will be passed in from a view:
{{ page_title }}
: This will be replaced with the title of the web page.{{ name }}
: This will be replaced with the name of the user.{{ message }}
: This will be replaced with a message that will be displayed on the web page.
Notice that we are using two curly barces for the context variables.Once the template folder has been set up and the path to it has been added to settings.py
, the next step is to create a view that will render the template files.
There are multiple ways to render the template files in django. But one common and my favourite way to do it is:
from django.template.loader import render_to_string
from django.http import HttpResponse
def example_view(request):
context = {
'message': 'Hello, World!',
'author': 'Anonymous',
}
rendered_html = render_to_string('example.html', context=context)
return HttpResponse(rendered_html)
Here, we're using the render_to_string
function from django.template.loader
to render the example.html
template. This function takes two arguments: the name of the template file (in this case, 'example.html'
), and a dictionary of context variables to be used in the template.
The context
dictionary in this example contains two key-value pairs: 'message': 'Hello, World!'
and 'author': 'Anonymous'
. These variables can be accessed in the template using Django's template language.
Once the template has been rendered, we return it as an HttpResponse
object. This will display the template in the user's browser when they navigate to the URL associated with the view.
This article provides a beginner-friendly guide on how to use templates in a Django project. It covers the definition of templates in Django, the necessary steps to create a template folder, how to add the relative path to the setting.py file, creating a template file, and rendering the template file using context. By following these steps, developers will be able to create templates in Django and use them appropriately, making it easier to maintain and update their application's user interface. However, it's worth noting that there are many other advanced features that Django's templating engine offers, such as template inheritance, custom template tags, and filters, which are not covered in this article but are worth exploring.
3 mins to finish this blog.
708 words read. Hooray!
Computer Science
Sulav Jung Hamal - 2024/08/20