...

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:

members/templates/index.html:
<h1>Members</h1>

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

Modify the View

Change the indexindex view to include the template:

members/views.py:
from django.http import HttpResponse
from django.template import loader
from .models import Members

def index(request>
: mymembers = Members.objects.all(>
.values(>
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 127.0.0.1:8000/members/in the address bar.