Free Ebook cover System creation course with Python and Django complete

System creation course with Python and Django complete

New course

176 pages

Creating a Django Project: Creating Templates

Capítulo 62

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Developing a Django project involves several essential steps, one of the most important of which is creating models. Models are the data abstraction layer that Django uses to structure your database information. They are a high-level representation of database tables and provide a convenient way to create, retrieve, update, and delete records.

Why Create Templates?

Templates are extremely useful because they allow you to work with data in a Pythonic way, rather than writing SQL queries. In addition, models also provide a convenient place to place methods related to your data. For example, if you have a User model, you might have a method that calculates the user's age based on their date of birth.

How to Create Templates?

Creating a model in Django is quite simple. First, you need to create a new Django application (if you don't already have one) using the command python manage.py startapp application_name. Then, inside the models.py file of this application, you can start defining your models.

A model is a subclass of the django.db.models.Model class, and each attribute of the class represents a field in the database. For example, if we wanted to create a template to represent a blog, we could do something like this:


from django.db import models

class Blog(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)

In this example, title, content and pub_date are fields in the database. The types of these fields (CharField, TextField, DateTimeField) determine the type of data they can contain.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Using the Templates

Once you've defined your models, you need to tell Django to create the corresponding tables in the database. This is done using the command python manage.py makemigrations followed by python manage.py migrate. The commands create a set of instructions for changing the database (migrations) and then apply those instructions.

Once the tables have been created, you can start using your templates to create, retrieve, update, and delete records. For example, to create a new blog, you could do the following:


from myapp.models import Blog

blog = Blog(title='My first blog', content='This is my first blog post.')
blog.save()

To retrieve all blogs, you could do:


blogs = Blog.objects.all()

In summary, creating models is an essential part of developing a Django project. Templates provide a convenient way to structure and manipulate your data, allowing you to focus on developing your application instead of worrying about the details of the database.

Now answer the exercise about the content:

What are templates in the context of a Django project and what is their purpose?

You are right! Congratulations, now go to the next page

You missed! Try again.

In Django, templates are used to dynamically generate HTML content based on data passed from views. They are different from models, which are the data abstraction layer. Therefore, option 5 that defines templates as a mechanism to generate HTML content would be correct. However, the provided options don't include this, and mistakenly define templates in terms of models. Models (option 2) provide an abstraction for database tables.

Next chapter

Creating a Django Project: Creating Views

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.