Render Form Field Manually – Django Form | Django Tutorials
June 12, 2021 2023-04-08 12:54Render Form Field Manually – Django Form | 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 manage.py makemigrations
After running this command you have to run another command that iwll create a table for you accoridng to your model class.
python manage.py 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
Rendering a Django Form in view.py
Open views.py file, import the form from forms.py which you want to render.
from .forms import SimpleForm
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”>
</form>
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.
as_table()
as_ul()
as_p()
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”>
</form>
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”>
</form>
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
{{form.name_of_field}}
e.g:
{{form.email.id}} will give you id of email field
{{form.email.name}} will give you name of email field
if you want to get value of name atribute then:
{{form.email.name.value}} 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 }}
e.g
{{form.email.error}} will give a error when user make for email field
If you want to get compelte label tag then you can use:
{{ form.name.label_tag }}
e.g
{{form.email.label_tag}}
or you want to get name of label then use:
{{ form.email.label }}
Conclusion
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