HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
In the previous chapter, we managed to display the content of a database table in a web page
In the previous chapter, we managed to display the content of a database table in a web page:
To add some HTML around the values, we will create a template for the application.
All templates must be located in the templates
folder
off your app, if you have not already created a
templates
folder, do it now.
In the templates
folder, create a file named
index.html
, with the following content:
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.
Template tags allows you to perform logic and render variables in your templates, you will learn more about template tags later.
Change the index
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 index
view does the following:
mymembers
object with all the values of the Members model.In the browser window, type 127.0.0.1:8000/members/
in the address bar.
The result: