Django Models and Forms

Naveen Singh
4 min readMay 12, 2022

--

We have covered till Templates in previous blog. To visit, click here.

We all know Django Framework is used for full stack development means Frontend and Backend. For frontend we can use React, JS, HTML etc.

Backend is used to store and retrieve data from backend database such as creation of user, login, registering, validation etc. When we talk about database there are lots of databases are present like SQL(RDBMS), NoSQL(MongoDB), Graphical etc. Which one to use is depends on developer and requirement. Django by default comes with SQLite and if you want to change the database go to settings.py then

give the name of preferred database as engine and tell where the database file resides. You also have to give value to the default variable for a particular database. For example if you want MySQL then first you should the library that connects MySQL to python then value for some default variable like “username, password”(if you don’t know how to connect SQL with Python then be a good boy and google it).

I’m taking the default database that is SQLite for this project. Now we have to learn how to create tables in SQLite through Django and connect to our web application.

Let’s go with the flow

Let’s take an example of Facebook, when you register for the first it ask you certain details like name, email, password the account created. When you try to login you have to fill the detail then hit the login button.

We are going to perform the same task for our project. A signup page then login page.

Before moving to the database we have to create a form for signup and login page. Django also know that this is a very common requirement so it makes it simple but how?

By providing forms.py file, you just have to create a new python file and name it forms in your web application folder. Then we have to import forms from django, lets see how:

You have a question that how I know what to import? The answer is “I google everything”. Now the main part starts how to create fields for forms?

forms.py

Let’s understand the above figure thoroughly:

  1. I created a class UserRegistration to use its instance/object in template.
  2. I passed forms.Form as argument to the class because forms.Form is acting like a metaclass for this class.
  3. Then three fields are created called name, email, password. Take Password field as a example: CharField → this is a input box of text type that can take alphanumeric character. max_length → is a argument that tells the maximum number of character for a particular field, for CharField type max_length argument is required. Label is equals to the <label> tag in HTML. Widget are the property that specifically affects that particular field that is password is a charfield so if a user types a password it is visible but when we apply widget then it appears as a black dot to secure the password.
  4. So we have created our forms let’s render it on our templates.
views.py
views.py
registration.html

and the results are here:

three fields without having a single input field in template

Now we can celebrate!!!

But if now a user fills the form nothing going to be happen as no data is received by the server.

To do that we have to use POST method in our form and also tweak views.py

Minor changes in registration.html:

{% csrf_token %} is a way to secure the data of form field.

When i click on register button see what data are going to server:

You can see the values of the particular field that I have submitted. POST is a method that has a dictionary where labels are key and values is given by user.

views.py after tweaking

--

--

No responses yet