Open source softwares - Django

Back to Course

Lesson Description

Lession - #998 Django-Add Template

Adding a Template to the Application

To add some HTML around the qualities, we will make a template for the application.

All template should be situated in the templates folder off your application, in the event that you have not previously made a templates folder, do it now.

In the templates folder, make a record named index.html, with the accompanying substance:


<table border="1">
{% for x in mymembers %}
<td>{{ x.id }}</td>
<td>{{ x.firstname }}</td>
<td>{{ x.lastname }}</td>
{% endfor %}
Did you notice the {% %} and {{ }} parts? They are called template tags.

Modify the View

Change the indexindex view to include the template:

from django.http import HttpResponse
from django.template import loader
from .models import Members

def index(request>
: mymembers = Members.objects.all(>
template = loader.get_template('index.html'>
context = { 'mymembers': mymembers, } return HttpResponse(template.render(context, request>

The record view does the accompanying:

  • Makes a mymembers object with every one of the values of the Members model.
  • Loads a the index.html layout.
  • Makes an item containing the mymember object.
  • Sends the item to the layout.
  • Outputs the HTML that is rendered by the template.

    In the browser window, type the address bar.