App Template

Inside the courses folder create a templates folder.

HTML Template

Inside the templates folder create another courses folder, and init create a course_list.html
[./courses/templates/courses/course_list.html]

{% for course in courses %}
    <h2>{{ course.title }}</h2>
    {{ course.description }}
{% endfor %}

{% %}
Used for control commands.

{{ }}
Used for printing.

course.title , course.description
You can access a record as you access an object’s attribute.


View

[./courses/views.py]

from django.http import HttpResponse
from django.shortcuts import render

from .models import Course 


def course_list(request):
  courses = Course.objects.all()
  return render(request, 'courses/course_list.html', {'courses': courses})

{'courses': courses}
The sent context from the view to the template.


Project Templates

1. Templates Folder

To create a homepage view, create a templates folder inside the learning_site project root folder.
Now, register the new templates folder in the project’s settings.py file
[settings .py]

...
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemlpates',
        'DIRS': ['templates', ], 
        'APP_DIRS': True, 
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ], 
        }, 
    }
]
...

BACKEND
In this field you can use the Jinja2 language for example instead of DjangoTemplates.

DIRS
Where you add your templates folder.


2. HTML Temlpate

In the new templates folder, create a home.html.
[./templates/home.html]

<h1>Welcome!</h1>

3. View

[./learning_site/views.py]

from django.shortcutes import render

def hello_world(request):
  return render(request, 'home.html')