How to Define Widget for Django Form Attribute in Django Application

Posted on

Introduction

This article itself is describing about how to define widget for Django form attribute in Django application. So, using a widget is possible in a page using a Django form exist in the Django application. Actually, the widget will be part of the Django form’s attribute. So, the content of this article will be about how to do it. Specifically, in order to do an actual declaration in the Django form. Before going into that,  just check several articles for preparation. It will show the steps for setting up the Django application before starting to define widget in one of the component of the Django form. Below is the preparation for simulating it :

  1. First of all, just install Python interpreter tool. This example will use windows as its operating system. For a reference, just check the article in How to Install Python in Microsoft Windows, How to Install Python 3.10 in Microsoft Windows or in How to Install Python in Microsoft Windows 11. It will describe how to install Python in a device running using Microsoft Windows operating system.

  2. After that, just make sure to have a python virtual environment to contain or to isolate the python environment just for the application. An article exist for showing that purpose exist in How to Create a Python Virtual Environment with a specific version of Python in Microsoft Windows or How to Specify Specific Python Version for Creating a Virtual Python Environment in Microsoft Windows.

  3. Do not forget to activate the python virtual environment after creating it.

  4. Install pip tool to be able to install necessary library for creating Django project and Django application. Look for it in How to Install pip in Microsoft Windows or in How to Install pip in Microsoft Windows 11 as a reference.

  5. Following after, just install the Django library. It is useful in order to create the Django project and also the Django application. There are also several articles showing series of those processes. For installing Django, it exist in How to Install Django in Microsoft Windows. On the other hand, for creating Django project, it is available in How to Create Django Project in Microsoft Windows using Command Line. And for creating Django application, it exist in How to Create a Django Application inside a Django Project in Microsoft Windows

  6. For the case in this article, there is  an example of the Django application using form. Just check How to Use ModelForm to Create a Form for submitting User Input in Django Application. But as a model reference for the Django form, below is the content of the models.py defining the model :

    from django.db import models
    # Create your models here.
    class Employee(models.Model):
        name = models.CharField(max_length=50)
        email = models.CharField(max_length=100)
        address = models.CharField(max_length=250)
        class Meta:
            db_table = 'employee'
    

 

How to Define Widget for Django Form Attribute in Django Application

So, how can a widget be a part of the form ?. Well, there is actually a simple way to be able to do that. Normally, the content of the form will exist as follows :

from django.forms import ModelForm
from django import forms
from .models import Employee

class EmployeeForm(ModelForm):
    
    class Meta:
       model = Employee
       fields = ['name','address','email']

    def __init__(self, *args, **kwargs):
       super(EmployeeForm, self).__init__(*args, **kwargs)
       self.fields['name'].label = 'Name'

But sometimes, using only a default component of the forms is not enough and it is not suitable to fulfill the requirement. The page will have a display in the following appearance :

How to Define Widget for Django Form Attribute in Django Application
How to Define Widget for Django Form Attribute in Django Application

One of the necessary adjustment is using another type of component for address field. For an example, using textarea for compoent instead of textfield. So, in ordert to do that, just use a widget. The following is the example using a widget of textarea to override textfield component. Just define it in the Django form as follows :

from django.forms import ModelForm
from django import forms
from .models import Employee

class EmployeeForm(ModelForm):
    address = forms.CharField(widget = forms.Textarea)
    class Meta:
       model = Employee
       fields = ['name','address','email']

    def __init__(self, *args, **kwargs):
       super(EmployeeForm, self).__init__(*args, **kwargs)
       self.fields['name'].label = 'Name'

For the above case, it is redefining the component which is going for further modification. In this context, it is by adding the following line as exist above :

    address = forms.CharField(widget = forms.Textarea)

In the above modification, changing the Django form by using a specific widget as textarea in the above script will eventually change the page display as follows :

How to Define Widget for Django Form Attribute in Django Application
How to Define Widget for Django Form Attribute in Django Application

 

Leave a Reply