Introduction
Django forms include a developer-friendly built-in feature, and Django has many different rendering build-in approaches. These methods, without a doubt, allow us to generate forms rapidly. Still, they also give us the least control over how they are rendered. There is no CSS or javascript in this HTML form. We can't have a user-friendly UI(user interface) without CSS, and we can't have a well-validated server-side form without javascript. So Django is wonderful for simple UI forms. Still, we render Django form fields manually to get a beautiful, user-friendly, more controlled Django template form.
Build a Django form scenarios
To understand the rendering of Django form fields manually through an example. Let’s consider a project having an app named test.
A new file, forms.py, can be created in our app to store all the forms. Django Form Class is required to develop a Django form. Let us see how.
forms.py
from django import forms
class TestForm(forms.Form):
name = forms.CharField(max_length = 200)
description = forms.CharField(max_length = 500)
contact_number = forms.IntegerField(
help_text = "Enter 10 digit mobile number"
)
password = forms.CharField(widget = forms.PasswordInput())
The field’s name is indicated on the left side, and the various functions of an input field are defined on the right side.
Create a data_view as shown to render this form into a view.
views.py
Construct an instance of the form class generated earlier in forms.py in view.
from django.shortcuts import render, redirect, reverse
from .forms import TestForm
def data_view(request):
context ={}
context['form']= TestForm()
return render(request, "data.html", context)
The below code has a form with a post method.
<form action = "" method = "post">
{% csrf_token %}
{{form }}
<input type="submit" value=Submit">
</form>

Output
As we can see, the UI is not very good, and it's also not a well-controlled form, which generates the need to render Django form fields manually.
as_table() renders the form using the template assigned to the forms template_name_table attribute. By default, this template is 'django/forms/table.html'.
as_ul() renders the form using the template assigned to the forms template_name_ul attribute. By default, this template is 'django/forms/ul.html'.
as_p() renders the form using the template assigned to the forms template_name_p attribute. By default, this template is 'django/forms/p.html'.
They all work similarly. The only difference is the HTML code surrounding the inputs.
Render Django form fields manually.
Let’s fix the form by rendering it manually to make it look impressive.
<html>
<head>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<style>
.i-am-centered {
margin: auto;
max-width: 350px;
padding-top: 25%;
}
</style>
</head>
<body>
<div class="i-am-centered">
<form method="POST">
{% csrf_token %}
<div class="form-group">
<label>First Name </label>
{{ form.name }}
</div>
<div class="form-group">
<label>description </label>
{{ form.description }}
</div>
<div class="form-group">
<label>Number</label>
{{ form.contact_number }}
</div>
<div class="form-group">
<label>Pwd</label>
{{ form.password }}
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
These were only some basic Bootstrap tweaks. Various CSS tricks and methods can be used to personalise it to an expert level.

output




