How to create and use templates in Django

Django is a powerful web framework that allows developers to create complex web applications quickly and easily. One of the most powerful features of Django is its template system, which allows developers to create and use templates to create dynamic web pages. In this tutorial, we will learn how to install Django, create a project, create an app, create a template, add template code, add the template to views, render the template, and use the template.

Install Django

The first step in creating and using templates in Django is to install Django. To do this, you will need to have Python installed on your computer. Once Python is installed, you can install Django using the command line. To do this, open a terminal window and type the following command:

pip install django

This will install the latest version of Django on your computer. Once the installation is complete, you can move on to the next step.

Create a Project

Once Django is installed, you can create a project. To do this, open a terminal window and type the following command:

django-admin startproject myproject

This will create a new project called “myproject” in the current directory. Once the project is created, you can move on to the next step.

Create an App

Once the project is created, you can create an app. To do this, open a terminal window and type the following command:

python manage.py startapp myapp

This will create a new app called “myapp” in the current directory. Once the app is created, you can move on to the next step.

Create a Template

Once the app is created, you can create a template. To do this, create a new file in the “templates” directory of your app. The file should have the same name as the app, and it should have the “.html” extension. For example, if your app is called “myapp”, the template file should be called “myapp.html”.

Add Template Code

Once the template file is created, you can add template code. Template code is written in HTML, and it is used to create the structure and content of the web page. For example, you can add a heading, a paragraph, and an image to the template:

<h1>My App</h1><p>This is my app.</p><img src="https://example.com/image.jpg" alt="My App">

You can also add template tags, which are special tags that allow you to add dynamic content to the page. For example, you can add a template tag to display the current date:

<p>The current date is {{ date }}</p>

Once you have added the template code, you can move on to the next step.

Add Template to Views

Once the template is created, you can add it to the views. Views are functions that are used to render the template. To add the template to the views, open the “views.py” file in the “myapp” directory and add the following code:

from django.shortcuts import renderdef myapp_view(request): return render(request, 'myapp.html')

This code will render the “myapp.html” template when the “myapp_view” view is called. Once the template is added to the views, you can move on to the next step.

Render the Template

Once the template is added to the views, you can render the template. To do this, open a terminal window and type the following command:

python manage.py runserver

This will start the development server, which will allow you to view the template in your web browser. To view the template, open a web browser and go to “http://127.0.0.1:8000/myapp/”. This will display the template in your web browser.

Use the Template

Once the template is rendered, you can use it to create dynamic web pages. To do this, you can add template tags to the template, which will allow you to add dynamic content to the page. For example, you can add a template tag to display a list of blog posts:

<ul>{% for post in posts %} <li>{{ post.title }}</li>{% endfor %}</ul>

This template tag will loop through a list of blog posts and display the title of each post. Once you have added the template tags, you can use the template to create dynamic web pages.

Conclusion

In this tutorial, we learned how to create and use templates in Django. We installed Django, created a project, created an app, created a template, added template code, added the template to views, rendered the template, and used the template. With these steps, you can create dynamic web pages using Django’s powerful template system.

Useful Links