Python Django Form Field Custom Widgets Example

Published On: 25/06/2022 | Category: Django Python

This article will provide example of python django form field custom widgets. I explained simply step by step python django form field custom widgets add. In this article, we will implement a python django form field custom widgets change. you'll learn python django form field custom widgets create.

The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type of data that is to be displayed

Let's see bellow example we will implement a python django form field custom widgets change.

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject exampleapp
Step 2 : Create a App
python3 manage.py startapp widgets

Next, you need to add it in the settings.py file as follows:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'widgets',
]
Default Widget in Form Fields

So, Every field has a predefined widget, for example IntegerField has a default widget of NumberInput.

Creating the FormClass

Now let’s create a demo form in widgets/forms.py”,

widgets/forms.py
from django import forms

# creating a django form
class PostForm(forms.Form):
    title = forms.CharField()
    content = forms.CharField()
    totalviews = forms.IntegerField()
    available = forms.BooleanField()
Creating the Views

Now to render this form we need to create the view and template which will be used to display the form to user. In widgets/views.py, create a view

widgets/views.py
from django.shortcuts import render
from .forms import PostForm

def home_view(request):
    context = {}
    form = PostForm(request.POST or None)
    context['form'] = form
    return render(request, "home.html", context)
Creating the Template

Next, open the home.html file and the add:

<h2>Python Django Form Field Custom Widgets Example - Tuts-Station.com</h2>
<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit">
</form>

Now let’s display the form by running

python manage.py runserver
Output
Custom Django form field widgets

Here, one can override the default widget of each field for various purposes. To override the default widget we need to explicitly define the widget we want to assign to a field.

Thus we can assign, any widget to any field using widget attribute.

Creating the FormClass

Now let’s create a demo form in widgets/forms.py”,

widgets/forms.py
from django import forms

# creating a django form
class PostForm(forms.Form):
    title = forms.CharField(widget = forms.CheckboxInput)
    content = forms.CharField(widget = forms.Textarea)
    totalviews = forms.IntegerField(widget = forms.TextInput)
    available = forms.BooleanField(widget = forms.Textarea)
Creating the Views

Now to render this form we need to create the view and template which will be used to display the form to user. In widgets/views.py, create a view

widgets/views.py
from django.shortcuts import render
from .forms import PostForm

def home_view(request):
    context = {}
    form = PostForm(request.POST or None)
    context['form'] = form
    return render(request, "home.html", context)
Creating the Template

Next, open the home.html file and the add:

<h2>Python Django Form Field Custom Widgets Example - Tuts-Station.com</h2>
<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit">
</form>

Now let’s display the form by running

python manage.py runserver
Output
Step 3 : Creating Urls

In this section, we’ll create the urls to access our views.Go to the urls.py widgets/urls.py file and update it as follows:

widgets/urls.py
from django.urls import path
from widgets import views

urlpatterns = [
    path('form', views.home_view),
]

Next, we will require the modify the urls.py your root preoject folder lets update the file.

autocomplete/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('widgets.urls')),
]

Now let’s display the form by running

python manage.py runserver
Output

I Hope It will help you....