Show Table Data In Table View

show table data in table view

Show Table Data In Table View

 |   |  0
Django Programming

In this article, we show how to print out all rows of a database table with Python in Django.

In this example, we have a database table and we’re going to show how to print out all rows and columns of the database table so that it can be displayed to the user.

So let's get right to it.

djangotestproject/views.py

from django.shortcuts import render
from django.http import HttpResponse
from djangotestapp.models import Mymodel
 
def showSiteInfo(request):
  infos= Mymodel.objects.all()
  context= {'infos': infos}
 
  return render(request, 'siteinfo.html', context)

djangotestproject/urls.py

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
 
urlpatterns = [
    path('afl-admin/', admin.site.urls),
    path('afl/hello/', views.aflhello, name='hello world'),
    path('afl/helloarg/', views.hello, name='hello world1'),
    path('afl/siteinfo/', views.showSiteInfo, name='Site Info'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

djangotestapp/models.py

from django.db import models
 
# Create your models here.
class Mymodel(models.Model):
	website      = models.CharField(max_length = 50)
	mail         = models.CharField(max_length = 50)
	name         = models.CharField(max_length = 50)
	phonenumber  = models.IntegerField()

	def __str__(self):
	  return self.name

	class Meta:
	  db_table = "afl_info"

djangotestproject/templates/siteinfo.html

<html>
<head>
<meta charset="UTF-8">
<title>Site Informations</title>
</head>
<body>
{% load static %}
<link rel="stylesheet" href="{% static '/bootstrap/css/bootstrap.css' %}">
 
<div class="container">
  <h2 class="pt-5">List of Sites</h2>
 
  <table class="table table-bordered table-light table-striped">
    <thead class="thead-dark">
      <tr>
        <th scope="col">#</th>
        <th scope="col">Name</th>
        <th scope="col">Website</th>
        <th scope="col">E-Mail</th>
        <th scope="col">Phone Number</th>
        <th scope="col">Edit</th>
        <th scope="col">Delete</th>
      </tr>
    </thead>
    <tbody>
 
    {% for info in infos %}
      <tr>
        <th scope="row">{{ forloop.counter }}</th>
        <td>{{ info.name }}</td> 
        <td>{{ info.website }}</td> 
        <td>{{ info.mail }}</td> 
        <td>{{ info.phonenumber }}</td> 
        <td><a class="btn btn-warning btn-sm" href="/afl-admin/djangotestapp/mymodel/{{ info.id }}/change">Edit</a></td>
        <td><a class="btn btn-danger btn-sm" href="/afl-admin/djangotestapp/mymodel/{{ info.id }}/delete/">Delete</a></td> 
      </tr>
    {% endfor %}
 
    </tbody>
  </table>
</div>
 
</body>
</html>

And this is all that is required to print out all rows of a database table with Python in Django.


0 Claps

Show your love in the form of Claps and Comments...

Comments...

No comments found. Leave your reply here.