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.


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)


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/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)


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):

	class Meta:
	  db_table = "afl_info"


<meta charset="UTF-8">
<title>Site Informations</title>
{% 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">
        <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>
    {% for info in infos %}
        <th scope="row">{{ forloop.counter }}</th>
        <td>{{ }}</td> 
        <td>{{ }}</td> 
        <td>{{ info.mail }}</td> 
        <td>{{ info.phonenumber }}</td> 
        <td><a class="btn btn-warning btn-sm" href="/afl-admin/djangotestapp/mymodel/{{ }}/change">Edit</a></td>
        <td><a class="btn btn-danger btn-sm" href="/afl-admin/djangotestapp/mymodel/{{ }}/delete/">Delete</a></td> 
    {% endfor %}

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...


No comments found. Leave your reply here.