Current Location: Home> Latest Articles> How to Quickly Implement CMS Table Generation Feature Using Python and Django

How to Quickly Implement CMS Table Generation Feature Using Python and Django

M66 2025-07-10

How to Build a Table Generation Feature in CMS Using Python

With the continuous advancement of information technology, Content Management Systems (CMS) play a crucial role in enterprise digital transformation. Tables are a common way to present data and are widely used in CMS. This article will explain how to use Python and the Django framework to dynamically generate and display tables within a CMS.

Environment Setup

Before starting, ensure you have Python installed along with the Django framework. Django offers a powerful Model-View-Template structure that is ideal for rapid CMS development. It uses models to define database structures, views to handle business logic, and templates for page rendering.

Database Design

In a CMS, table data needs to be structured and stored properly. Below is an example Django model defining the table name, number of rows and columns, and timestamps:

from django.db import models

class Table(models.Model):
    name = models.CharField(max_length=100)
    columns = models.IntegerField()
    rows = models.IntegerField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

This model sets the foundation for managing and retrieving table data.

Table Generation

The view layer should query the database for all table records and pass them to the template for rendering. Here is a sample view function:

from django.shortcuts import render
from .models import Table

def table_view(request):
    tables = Table.objects.all()
    return render(request, 'table.html', {'tables': tables})

This function retrieves all table entries and sends them to the template named table.html.

HTML Template

The template uses Django's template syntax to loop through the table data and dynamically create the table structure, as shown below:

<!DOCTYPE html>
<html>
<head>
    <title>CMS Table Display</title>
</head>
<body>
    {% for table in tables %}
    <h2>{{ table.name }}</h2>
    <table>
        <thead>
            <tr>
                {% for i in range(table.columns) %}
                <th>Column {{ i+1 }}</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for j in range(table.rows) %}
            <tr>
                {% for k in range(table.columns) %}
                <td>Row {{ j+1 }}, Col {{ k+1 }}</td>
                {% endfor %}
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% endfor %}
</body>
</html>

The nested loops render table headers and body cells dynamically, ensuring flexible and clear table layouts.

Testing and Running

After development, start the Django built-in server for testing. From the project root directory, run:

python manage.py runserver

Open a browser and navigate to http://localhost:8000/table to view the generated tables and verify functionality.

Conclusion

This article demonstrated a complete process for implementing table generation in a CMS using Python and Django, covering environment setup, database modeling, view development, template rendering, and testing. Following these steps allows developers to quickly build dynamic table features that enhance CMS usability and user experience.

References

  • Django Official Documentation: https://docs.djangoproject.com/
  • w3schools Django Tutorial: https://www.w3schools.com/python/python_django.asp