Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to render Django forms as a table

Njoku Ifeanyi Gerald

What are Django forms?

Django forms are similar to advanced HTMLHyperText Markup Language forms, except that they are more Python-oriented in nature.

To render Django forms as a table, you need to use the {{ form.as_table}} command.


First, you will need to install Django, which can be done using the following commands:

pip install pipenv
pipenv shell
pipenv install django

Next, create your Django project:

django-admin startproject DjangoForm ./
python startapp codebase
python migrate
python runserver


First, you will need to add the project name to go to the file, as shown below.



Note: codebase should be replaced by the name of your Django project.

Next, in the codebase application folder, go to the file and create the following model:

from django.db import models

# Create your models here.
class DataModel(models.Model):
    name = models.CharField(max_length = 200)
    department = models.CharField(max_length=300)
    age = models.IntegerField()

    def __str__(self):

Once your model is created, you need to register it in the file, as shown below:

from django.contrib import admin
from .models import DataModel

Create the form

With the configuration complete, you can create a file for the form. In our case, the file is named

from django import forms

class DataForm(forms.Form):
    name = forms.CharField(max_length = 200)
    department = forms.CharField(max_length = 200)
    age = forms.IntegerField(
                    #  help_text = "Enter 6 digit roll number",

With the form created, you can now create a view for your project. The corresponding file is shown below:

from django.shortcuts import redirect, render
from .models import DataModel
from .forms import DataForm

def home(request):
    form = DataForm(request.POST or None)
    if request.method == 'POST':
        # form.is_valid() make the form to submit only 
        # when it contains CSRF Token
        if form.is_valid():
            # form.cleaned_data returns a dictionary of validated form input fields
            name = form.cleaned_data['name']
            department = form.cleaned_data['department']
            age = form.cleaned_data['age']
            queryset = DataModel(name=name,department=department, age=age)
            return redirect('home')
    context = {
    return render(request, 'app/home.html', context)

Next, in the codebase app folder, create a folder and name it templates. Inside the templates folder, create another folder and name it app. Inside the app folder, create the home.html file as shown below:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <form action='.' method="POST">
        {% csrf_token %}
        {{ form.as_table }}
        <input type="submit" value="Submit">

Set route URLs

Within the main folder, in the file, you need to add a route to the URLs of the codebase app, as shown below:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    # including the codebase file
    path('', include('codebase.urls'))

Finally, in the codebase folder, create a file called that contains the route to the home view:

from django.urls import path
#importing home function from
from .views import home

urlpatterns = [
    path('', home, name='home'),

Access form

Once the entry process is complete, run the following commands:

python makemigrations

python migrate

In case you want to create a super-user, run the following command.

python createsuperuser

Once you’re ready to access your newly created form, run this command:

python runserver

The command above starts your server on the URL, which you can now access.




Njoku Ifeanyi Gerald

View all Courses

Keep Exploring