Django Starter 4 — Showing Table Data

Rubaiyat Rahim
3 min readMay 25, 2024

--

Showing database table data without using Admin panel.

Other parts of this series: Part 1 | Part 2 | Part 3 | Part 5 | Part 6

Adding a List Page

Create Table/Model

  • Create the model class (Player) in models.py in the app folder (myproject\players\models.py) as follows:
from django.db import models

class Player(models.Model):
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
dateofbirth = models.DateField(null=True)
phonenumber = models.CharField(max_length=50)
  • Create migration, then migrate.
py manage.py makemigrations
py manage.py migrate

Create Template

  • Create the template file all_players.html in the templates folder as (myproject\players\templates\all_players.html) to show all the Player objects from the Players table as follows:
<!DOCTYPE html>
<html>
<body>

<h1>Players</h1>

<ul>
{% for x in allplayers %}
<li>{{ x.firstname }} {{ x.lastname }}</li>
{% endfor %}
</ul>

</body>
</html>

Create View

  • Create the Players view in the players folder (myproject\players\views.py) as follows:
from django.http import HttpResponse
from django.template import loader
from .models import Player

def players(request):
allplayers = Player.objects.all().values()
template = loader.get_template('all_players.html')
context = {
'allplayers': allplayers,
}
return HttpResponse(template.render(context, request))
  • List of Players will be shown as follows:
Fig: List of Players.

Adding a Detail Page

Create Template

  • Create the template file details.html in the templates folder (myproject\players\templates\details.html) as follows:
<!DOCTYPE html>
<html>

<body>

<h1>{{ theplayer.firstname }} {{ theplayer.lastname }}</h1>

<p>Phone: {{ theplayer.phonenumber }}</p>
<p>Date of Birth: {{ theplayer.dateofbirth }}</p>

<p>Back to <a href="/players">Players</a></p>

</body>
</html>

Add Link in List Page

  • Insert a link in the list of all players (myproject\players\templates\all_players.html) as follows:
<!DOCTYPE html>
<html>
<body>

<h1>Players</h1>

<ul>
{% for x in allplayers %}
<li><a href="details/{{ x.id }}">{{ x.firstname }} {{ x.lastname }}</a></li>
{% endfor %}
</ul>

</body>
</html>

Create View

  • Add a details view function in the views.py (myproject\players\views.py) as follows:
from django.http import HttpResponse
from django.template import loader
from .models import Player

def players(request):
allplayers = Player.objects.all().values()
template = loader.get_template('all_players.html')
context = {
'allplayers': allplayers,
}
return HttpResponse(template.render(context, request))

def details(request, id):
theplayer = Player.objects.get(id=id)
template = loader.get_template('details.html')
context = {
'theplayer': theplayer,
}
return HttpResponse(template.render(context, request))

Add URL

  • Add the url in myproject\players\urls.py as follows:
from django.urls import path
from . import views

urlpatterns = [
path('players/', views.players, name='players'),
path('players/details/<int:id>', views.details, name='details'),

]
  • List of Players is now being shown with link to the Players details page as follows:
Fig: List of Players with Details link.
  • When the Details link of Players are clicked, details of that Player is being shown as follows:
Fig: Details of Players.

--

--