What are Django Forms?

Search for a command to run...

No comments yet. Be the first to comment.
This is a basic Django tutorial in which we will create an Authentication App, from scratch in Django 2.0 and Learn the Fundamental concepts of Django Framework.
In this section, we will learn what are Views in Django and how to use views.py to display webpage onto the browser on a web request. So in our previous section, we have successfully created our UserCreateForm form. Let's see what are Views in Django...
You've done it. I've done it. We've all done it.

Your AI Agent Has Amnesia. ByteRover Fixes It — Without Vectors Let's see how to built a customer support agent that remembers everything — using plain markdown files, BM25 search, zero embeddings, a

My GenAI cohort kicked off today, and what a first day! Right away we were reminded that AI isn’t magic — at its core, models like Generative Pretrained Transformers (GPT) are doing one simple thing:

Hello Folks👋, I Recently got Graduated🎓 in 2020, yep in the Pandemic. We all hit very hard by this Corona Pandemic and we all got stuck at home for very long. We all realize the Importance of Socializing, Fitness (Physically as well Mentally), Diet...

So after learning the basic commands of git in our previous section, let's see Git in action. If you already have a project to track your files then you can skip this section and if not then continue. Initializing a Repository from Non-existing Dire...

In this tutorial, we will learn what are Forms in Django and how to use forms.py to connect with models to directly save the result into the database table.
So in our previous section, we have successfully created our User model. Let's see what are Forms in Django.
In Django, With forms we will have absolute power over our interface -- we can do almost anything we can imagine! Creating forms in Django is really similar to creating a model. Here again, we just need to inherit from Django class and the class attributes will be the form fields.
The nice thing about Django forms is that we can either define one from scratch or create a ModelForm which will save the result of the form to the model.
Cool, that's exactly what we want to do, we will create a form for our "User" model. The forms have their own file forms.py. We need to create a file with this name in the account/ directory.
account
└── forms.py
OK, let's open it in the code editor and type the following code:
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
class UserCreateForm(UserCreationForm):
name = forms.CharField(label="Full Name", widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': ''}))
phone_no = forms.CharField(label="Mobile No", widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': ''}))
class Meta:
fields = ("name","username", "email","phone_no", "password1", "password2")
model = User
#to override the default label
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["username"].label = "User Name"
self.fields["email"].label = "Email address"
We need to import Django forms first (from django import forms), UserCreationForm from (from django.contrib.auth.forms import UserCreationForm) and our User model (from django.contrib.auth.models import User).
UserCreateForm, as you probably suspect, is the name of our form. We need to tell Django that this form is a UserForm(so Django will do some magic for us) -- UserCreationForm is responsible for that.
Next, we have class Meta, where we tell Django which model should be used to create this form (model = User).
Finally, we can say which field(s) should end up in our form. In this scenario, we want the following fields name, username, email,phone_no, password1, password2 which are already defined by Django.
And that's it! All we need to do now is use the form in a view and display it in a template.
In this section, we have successfully created forms in Django. So in the next section, we will see what are Views in Django.