TechAE Blogs - Explore now for new leading-edge technologies

TechAE Blogs - a global platform designed to promote the latest technologies like artificial intelligence, big data analytics, and blockchain.

Full width home advertisement

Post Page Advertisement [Top]

Django Forms

Introduction

Let’s see the example of Python Django class-based forms. In this tutorial, you’ll learn how to create forms using Django, We will be using the Class-Based Views Functions as every form is expected to follow the POST method, if the submitted form is valid then the data would save in the database. This is the most powerful technique to create Django forms. So let's start with this awesome
tutorial.

For Django setup, you can also follow this blog.

Django Project Setup

Make a folder and open cmd from that folder. Use the following commands step by step


1. To create isolated python environments


python -m venv venv


2. To activate the virtual environment


venv\Scripts\activate


3. Enter the Django command after you've established and activated a virtual environment


pip install django


4. To make a project directory


django-admin startproject projectname


5. Go to the project directory


cd projectname


6. Create the Django app


python manage.py startapp appname


7. Open the project


code .


8. Migrate and create Django user


python manage.py migrate
python manage.py createsuperuser


9. Start the Django server


python manage.py runserver


10. Check admin on this URL


http://127.0.0.1:8000/admin


11. Register your app in projectname/settings.py


INSTALLED_APPS = [

    'appname.apps.AppnameConfig',

    ... # Leave all the other INSTALLED_APPS

]


Django class-based forms

1. Create a model in appname/models.py


class Data(models.Model):

    name = models.CharField(max_length=60)

    body = models.CharField(max_length=200)

 

    def __str__(self):

        return self.name


2. Migrate model on Admin


python manage.py migrate
python manage.py makemigrations


3. Register database model on appname/admin.py


from .models import Data


admin.site.register(Data)


4. Making appname/forms.py. You can see here that we've imported forms and models. I'm going to use class meta to generate all the fields from the database. So here, I define the fields that I want to use.


from django import forms

from .models import Data


class DataForm(forms.ModelForm):

   class Meta:

     model = Data ## Database Model name 

     fields = ('name', 'body')


5. Register forms.py in appname/views.py. Here we're going to just validate the form and then perform an action. We're going to save the data if the submitted data is valid.


from django.shortcuts import render from appname.forms import DataForm def form_detail(request): if request.method == 'POST': form = DataForm(request.POST) if form.is_valid(): form.save() form = DataForm() return render(request,'contact.html', {'form' : form})


6. In App folder make the templates folder and create the file name appname/templates/contact.html


<form method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="Send message"> </form>


7. Adding path to appname/urls.py


from django.urls import path from appname import views urlpatterns = [ path('', views.form_detail, name="form") ]


8. Include your app name in the projectname/urls.py


from django.urls import include

path( '' , include( 'appname.urls'))


9. Restart the server


python manage.py runserver

Thank you for reading!

So we found a clear method to create forms using class-based Django. I hope everything is clear and helpful for you. Feel free to comment if you have any queries.

If you found this article useful, stay tuned for more articles on Django.

Cheers!✌

No comments:

Post a Comment

Bottom Ad [Post Page]