[PYTHON] How to set the html class attribute in Django's forms.py

Thing you want to do

I want to specify the ** class attribute ** that is set in the ** html tag ** when I write the form to be displayed in the template with ** forms.py **.

forms.py


class Form(forms.Form):
    sample = forms.IntegerField(label="hoge")

If this is left as it is, the output will be as follows, and the ** class attribute ** cannot be set in the ** input ** tag.

<input type="number" name="sample" required="" id="id_sample">

solution

Override the ** \ _ \ _ init \ _ \ _ ** methods in ** forms.py **.

forms.py


class Form(forms.Form):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['sample'].widget.attrs['class'] = 'name of the class'

    sample = forms.IntegerField(label="hoge")

There are two points to note.

When to use

Use this when you want to adjust the appearance with ** css ** when you define a form with ** forms.py **.

reference

stackoverflow question https://stackoverflow.com/questions/401025/define-css-class-in-django-forms

Referenced answer https://stackoverflow.com/a/401057

Questioner https://stackoverflow.com/users/22306/ashchristopher

Respondent https://stackoverflow.com/users/22306/ashchristopher

Recommended Posts

How to set the html class attribute in Django's forms.py
How to use the exists clause in Django's queryset
How to use the __call__ method in a Python class
How to set the output resolution for each keyframe in Blender
How to use the render function defined in .mako (.html) directly in mako
[Introduction to Python] How to use class in Python?
How to set the server time to Japanese time
How to use __slots__ in Python class
How to view images in Django's Admin
How to use the C library in Python
How to get the files in the [Python] folder
How to uniquely identify the source of access in the Django Generic Class View
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
How to get the "name" of a field whose value is limited by the choice attribute in Django's model
How to specify a schema in Django's database settings
How to retrieve the nth largest value in Python
How to get the variable name itself in python
How to run the Ansible module added in Ansible Tower
The easiest way to set up Last-Modified in Flask
How to get the number of digits in Python
How to know the current directory in Python in Blender
How to get a logged-in user with Django's forms.py
Set the form DateField to type = date in Django
How to use bootstrap in Django generic class view
[Blender] How to dynamically set the selection of EnumProperty
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to upload files in Django generic class view
How to use the model learned in Lobe in Python
Do not pass self to ProcessPoolExecutor in the class
[Python] How to output the list values in order
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
Define a task to set the fabric env in YAML
How to write custom validations in the Django REST Framework
How to find the optimal number of clusters in k-means
[python] How to check if the Key exists in the dictionary
How to debug the Python standard library in Visual Studio
How to import Python library set up in EFS to Lambda
How to manipulate the DOM in an iframe with Selenium
How to log in automatically like 1Password from the CLI
How to generate a query using the IN operator in Django
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
How to use Django's GeoIp2
How to set the extended iso8601 format date to the Dataframe index
In Django, how to abbreviate the long displayed string as ....
How to develop in Python
Notes on how to use marshmallow in the schema library
[Shell] How to get the remote default branch in Git
How to use the decorator
How to increase the axis
How to start the program
How to intentionally issue an error in the shell During testing
How to handle multiple versions of CUDA in the same environment
[sh] How to store the command execution result in a variable
How to determine the existence of a selenium element in Python
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
How to get all the possible values in a regular expression
How to give and what the constraints option in scipy.optimize.minimize is
How to check the memory size of a variable in Python
Output the contents of ~ .xlsx in the folder to HTML with Python