Render Form Field Manually – Django Form | Django Tutorials

Render Form Field Manually
Django Django Tutorials

Render Form Field Manually – Django Form | Django Tutorials

Render Form Field Manually – Django Form | Django Tutorials

We have discussed different topics related to Django Form that what is Django form, its rendering, retrieving data, CSRF token in form, etc. But now, we have to learn that how we can Render Form Field Manually. It is a very easy way to do it just you have to read from start to end. We will discuss it with examples and explanations. 

Actually, Form is the collection of different types of fields in which users input different types of data. That data is reached to the webmaster in the database.

If we discuss its importance on the web, then I will say that you cannot learn one thing in web development without learning Form. To develop a basic website or application you must need to learn how to create and use form.

So, in Django, there is a comprehensive discussion on Form in different ways. 

Create a Django Model for ModelForm

We are staring to Render Form Field Manually, then you have to create a model class on the basis we will create a ModelForm, then we will render in the views function.

from django.db import models

class Simple(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=300,)
street = models.CharField(max_length=50, blank=True, null=True,)
postal_code = models.CharField(max_length=20 , blank=True, null=True, )
city = models.CharField(max_length=30, blank=True, null=True, )
state = models.CharField(max_length=20, blank=True, null=True,)

After writing abvoe model class you have to run a following command to generate a SQL coding for your database table but it will not create table for you. To applyment on your SQL generated coding you have to run another comand, that i will mention here

python makemigrations

After running this command you have to run another command that iwll create a table for you accoridng to your model class.

python migrate

How to create Django ModelForm

Now, after creating model class, you need to create modelForm according to model named Simple. It will create a  Form, that you need to render in the view function. Then you have to render in the template,

from django.forms import ModelForm

from .models import Simple

class SimpleForm(forms.ModelForm):
    class Meta:
        model = Simple
        fields = ‘__all__’

Rendering a Django Form in

Open file, import the form from which you want to render. 

from .forms import SimpleForm

def jafriform(request):
    if request.method == “POST”: # Check if the method is POST
        form = SimpleForm(request.POST) # Then dispaly the form
        if form .is_valid(): # if form contain valid data
            form .save() # then save the form data in the database
    else: # if methods is not POST  
            form = SimpleForm() # then dispaly the form as blank 
        context = {‘form ‘:form} # make dictioanry making key a form.
    return render(request, ‘users/jafriform.html’, context) # pass the context including form key to template

Rendering a Django Form in the template

Then go to your template file in whcih you want to render a Form. 

<form action=”/your-name/” method=”post”>
{% csrf_token %}
{{ form }}
<input type=”submit” value=”Submit”>

Render Form Field Manually

The forms API offer three methods to automatically render the HTML form as in the table form, ul (unordered list) form and in the paragraph form. 


When you run the program, you will notices that, the form fieds are display in the table form. 

<form action=”/your-name/” method=”post”>
{% csrf_token %}
{{ form.as_table }} 
<input type=”submit” value=”Submit”>

If you use followign source code then, form fields will show as unordered list. it is same like if you want to dispaly the form feilds in the paragrpah form. Just of {{form.as_p()}}

<form action=”/your-name/” method=”post”>
{% csrf_token %}
{{ form.as_ul() }} 
<input type=”submit” value=”Submit”>

Django Form renders the individual field

In this section, we will learn how you can get the individual fields from Django Form. There are different ways you can get any of the tags from the form you build. For example, if you want to get the name value of the email field, then you have to write one line of code to do, or getting id, class, etc you can do it easily. In the following example, we will discuss:

We can get all the attribute of the name of a field like its id, name value, class etc through following variable 



{{}} will give you id of email field

{{}} will give you name of email field

if you want to get value of name atribute then:

{{}} will give you value of name attribute e.g name=”this text you will get”

If you want to get error, then you can set error message for indivudailly fields as:

{{form.name_of_field,errors }}


{{}} will give a error when user make for email field

If you want to get compelte label tag then you can use:
{{ }}


or you want to get name of label then use:

{{ }}


To Render Form Field Manually is very easy and important to learn because you have to use it while developing the Django project. You can use it if you want to test some fields or want to perform some operations etc.

You can use JavaScript to perform operations on a specific field getting that field’s id. If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible.

If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn. 

Recommended Django Tutorial for Beginners

Complete Guide on Django ORM | Django Tutorials

What is Django URL Tag in Django Template Language

Hack – Django Website Tutorials | Building Complete Website with Django

How to Setup Django Static files in Django Project and Application

Complete Built-in Django Form Fields with Examples | Django Tutorials

Leave your thought here

Your email address will not be published. Required fields are marked *

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar

Get your Enrollment


Leave your details below and receive a discount coupon in your inbox