First template.

Before you create a template file to store your static HTML file.
You must have a folder to manage files.

~$ cd Opensource/opensource_XX/

~/Opensource/opensource_XX$ mkdir templates

After this, now you can create any template you want,
just remember to put it into folder to make sure view.py can get it.

~/Opensource/opensource_XX/templates$ vim index.html

use vim / nano or any other editor you like.


First view.

Every pages has it's own view function.
If we want to appropreate render a page with right data,
we have to edit view.py first.

~$ Opensource/opensource_XX/view.py

And this is your view.py
It sould be empty, and only one import inside.

from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'opensource_XX/templates/index.html', local())

Template Rendering.

Local variable render:

In view.py render function:

import datetime
from django.shortcuts import render
import datetime

# Create your views here.
def index(request):
    now_time = datetime.datetime.now()
    staff = [
            'opensource_01', 'opensource_02', 'opensource_03', 'opensource_04', 'opensource_05',
            'opensource_06', 'opensource_07', 'opensource_08', 'opensource_09', 'opensource_10',
            'opensource_11', 'opensource_12', 'opensource_13', 'opensource_14', 'opensource_15',
            'opensource_16', 'opensource_17', 'opensource_18', 'opensource_19', 'opensource_20',
            'opensource_21', 'opensource_22', 'opensource_23', 'opensource_24', 'opensource_25'
            ]

    return render(request, 'opensource_XX/templates/index.html', locals())

now_time is the local() variable in function index.
and it will pass to django's render engine by locals()


In opensource_XX/templates/index.html

Variable render

{{ now_time }}

this shows content of now_time variable.

if statement

{% if (condition) %}
    ....
{% endif %}

For loop statement and render

{% for each in staff %}
    <div>{{ each }}</div>
{% endfor %}

results matching ""

    No results matching ""