Django Creating Forms with Form Class

This is our fifteenth article in Django, in this article we are going to learn about Django Creating Forms with Form Class , so there are two ways that you can create Forms in Django, the first way is that you can use Form class and the second way is by using Model Form class, particularly in this article we are going to use Form Class.

 

 

 

Flask Web Development Tutorials

1: Flask CRUD Application with SQLAlchemy 

2: Flask Creating News Web Application 

3: Flask Creating REST API with Marshmallow 

 

 

 

Python GUI Development Tutorials

1: PyQt5 GUI Development Tutorials

2: Pyside2 GUI Development Tutorials

3: wxPython GUI Development Tutorials

4: Kivy GUI Development Tutorials 

5: TKinter GUI Development Tutorials

 

 

 

 

So now we need to create a New Project in Django, also for django installation and creating New Project you can read this article Django Introduction & Installation. but you can use this command for creating of the New Project.

 

 

 

After creating of the Django Project, you need to migrate your project. make sure that you have changed the directory to the created project.

 

 

 

 

 

Now you will receive this output in the terminal.

 

 

 

 

 

So after doing this, let’s create our Django App, i have already talked about Django App in one of my articles, you can check this Django Apps & URL Routing.

 

 

 

 

After creation of the App, you need to add the created App in your settings.py file INSTALLED_APP section .

 

 

 

 

Now open your models.py in your MyApp App, and add your model, this is a basic model with four fields, you can read my article on django models  Django Models Introduction.

 

 

 

 

 

After adding a new model in Django, first you need to create migrations file and after that you need to add your model to the database, by default Django uses Sqlite database, for mysql database connection you can read this article  Django Mysql Database Connection. make sure that you have changed directory to your project.

 

 

 

 

Now you will see this output in the terminal.

 

 

 

 

It is time to add our this model to the database, so for this you need to do migrate.

 

After this our model data has been successfully added to the Sqlite database.

 

 

 

 

OK now let’s just add our model to the Django admin panel, you can read this article for django super user   Django Creating Super User. open your admin.py file and add this code.

 

 

 

 

It’s time to create our form, now you need to create a new Python file in your app, i have named forms.py, but you can name it what ever you want.

 

 

 

 

 

Before adding our html files, we need to create our views, so open your views.py file and add this code for creating a simple view. and you can see in the view we have passed the form as context variable to the django template.

 

 

 

 

Now we are going to render this form in our html file, so you need to create a new directory in your Django Project called templates, and in the templates folder add two html files, the first one is index.html and the second one is base.html. for django templates you can read this article Django Templates Introduction. also make sure that you have added your templates name in the templates section of settings.py file like this.

 

 

 

 

 

This is our base.html file, also you can see that in the base.html we have added the Bootstrap CDN link, because in this article we are going to use from Bootstrap styles. also you can check this article for adding styles to your Django Project Django Adding Bootstrap & CSS.

 

 

 

 

 

This is our index.html file and we are going to render our simple form in here.

 

 

 

 

Also you need to create your urls, you can read this article for url routing Django Apps & URL Routing . but just create a new python file in your MyApp app at name of urls.py and add these codes. you can see that we have added our one url for the registration view in here.

 

 

 

 

 

And also you need to include your app urls.py in your project urls.py file.

 

 

 

 

Now you can run your project and we have our simple form.

 

 

 

 

http://localhost:8000/

Django Creating Forms with Form Class
Django Creating Forms with Form Class

 

 

 

 

Now let’s add some users from our form to  our database, OK we have already created our model at the top. open your views.py and add a new view function for adding data . you can see along with our previous view, we have added a new view function. basically we are going to create the object of our form, than we are checking that if our form is valid, if it was valid we are simply adding the data from our form to the database.

 

 

 

 

Also you need to add this view function in your urls.py file.

 

 

 

 

Open your index.html and bring some changes, first we have added our new url name to the form action, also when you are going to work with the forms, you need to add csrf_token in your html file.

 

 

 

 

 

Now you can run your project, add some data using the form. after that check your admin panel you will see the registered users.

 

 

 

 

http://localhost:8000/admin/MyApp/registrationdata/

Django Admin Panel
Django Admin Panel

 

 

 

 

 

Converting HTML Form to Django Form

If you take a look at our form design it is not looking good, now we are going to add some design from Bootstrap forms and after that we convert our form to Django Form.

 

Now we are going to bring some changes in our index.html file, and we need to remove our previous form and add some stylish form from the Bootstrap.

 

 

 

 

 

Now if you run the project this will be the result

 

http://localhost:8000/

Django Bootstrap Form
Django Bootstrap Form

 

 

 

 

 

Now let’s convert this form to Django form.

 

 

First of all open your forms.py file and bring changes to your form like this.

 

 

 

 

Also you need to bring changes in your index.html, so open that and add this code.

 

 

 

 

So now run the project and you can see that we have a nice form, but this time it is a Django form not Bootstrap form , and now you can add users to your database.

 

 

 

 

 

http://localhost:8000/

Django Creating Forms with Form Class
Django Creating Forms with Form Class

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×