{{form.as_table}} — display Django forms as a table

Django Forms — it is an advanced set of HTML forms that you can create with Python and that support all the features of HTML forms in Python. Rendering Django forms in a template can sometimes seem messy, but with the right knowledge of Django forms and field attributes, you can easily create a great form with all the powerful features. In this article, the form is displayed as a table in the template.

{{form.as_table}} — render Django forms as a table

Illustration {{form.as_table}} using an example. Consider a project named pythonengineering that has an application named geeks .

Refer to the following articles to check how to create a project and an app in Django.

Let`s create a sample Django form to display and show as an example. In geeks & gt; forms.py enter the following code

from django import forms

# create a form

class InputForm (forms.Form):


first_name = forms.CharField (max_length = 200 )

last_name = forms.CharField (max_length =  200 )

roll_number = forms.IntegerField (

help_text = "Enter 6 digit roll number"


password = forms.CharField (widget = forms.PasswordInput ())

Now we need a View to render this form to a template. Let`s create a view,

from django.shortcuts import render

from . forms import InputForm

# Create your views here.

def home_view ( request):

context = {}

context [ `form` ] = InputForm ()

  return render (request, " home.html " , context)

Finally, we will create a template where we need to place the form. In templates & gt; home.html ,

& lt; form action = " " method = "post" & gt; 

{% csrf_token%}

& lt; table & gt; 


& lt; / table & gt ; 

& lt; input type = "submit" value = "Submit" & gt; 

& lt; / form & gt; 

Here {{form.as_table}} will display them as table cells, wrapped in & lt; tr & gt; tags. Let`s check if this works accordingly or not. Open http:// localhost: 8000 /

Let`s check the source code, whether the form is displayed as a table or not. Rendering as a table means that all input fields will be wrapped in & lt; tr & gt; tags. 
Here is a demo,

Get Solution for free from DataCamp guru