Your first template - 30 days of Django

Your first template - 30 days of Django

/ #Django


Let's learn how to set up a simple template, and show information from a view.

A short introduction to templates

A template is usually just a simple HTML file. You use HTML just as you're used to, and then Django makes it possible to use different tags and blocks to make the templates dynamic. Django uses a template language very similar to twig, ninja and similar. So it's very easy to understand.

The front page template

Let's set up the template for the front page. This will make it easier to understand what's going on. First, create a new folder called "templates" in the "task" app folder, and then a folder called "task" inside the templates folder.

Django automatically tries to find a folder called "templates" in each of the app folders. And the reason why we have a new folder inside there called "task" is to make it easier to separate the apps later (You will understand when we get there).

Next, you can create a file called "frontpage.html" in the last task folder you created. It should look like this:

<div class="frontpage">
    <h1>{{ title }}</h1>

    <p>This is just a hard coded paragraph!</p>
</div>

That wasn't hard?

Most of this is just pure HTML. The only Django thing here is the h1 tag with curly braces inside. This is called a Django tag.

The "title" inside the two curly braces points to the "title" variable we passed in to the render function in the previous part of this series.

Summary

So now you've created your first Django template :-D
In the next part of this series, we will finally test the project and see it in action in a web browser.

Table of contents

Comments

No comments yet...

Add comment

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.