Skip to main content

Write Django Forms

Why Django Forms

Handling forms is a complex business. Consider Django’s admin, where numerous items of data of several different types may need to be prepared for display in a form, rendered as HTML, edited using a convenient interface, returned to the server, validated and cleaned up, and then saved or passed on for further processing.

Introduction to Django Forms

Django forms provide a way to handle user input in a Django web application. Forms can be used to validate user input, sanitize data, and save data to a database.

Form Class

In Django, a form is a Python class that inherits from the django.forms.Form class or one of its subclasses. A form is defined by specifying the fields it contains, as well as any validation or cleaning logic. When a form is submitted, the data entered by the user is passed to the form, and the form performs any validation or cleaning required. If the data is valid, it can be processed further or saved to a database.

Django Form Fields

Django provides several built-in form fields, such as CharField, IntegerField, EmailField, and BooleanField, among others. You can also define custom form fields if you need to handle data in a specific way.

Django Form Rendering in HTML

Django forms can be rendered as HTML forms using the as_p, as_table, or as_ul methods, which generate HTML code that can be included in a web page. You can also customize the appearance of forms by using CSS or by rendering the form fields manually.

Django Multimedia Form Handeling

Django also provides support for handling file uploads through forms, using the FileField and ImageField form fields. When a file is uploaded, Django handles the file data and saves it to a specified location on the server.

Write Django Forms

I guess above intro is enough to understand its functionality and need. Lets write first Django Form for User.

tip

Django forms must be in forms.py file which should be in app root directory.

Add following on the top of forms.py

blog/forms.py
from django import forms
from .models import *

User Form

Django must relate a form with a model. In this context, User form will be related to User model. Let's have a look into the code.

blog/forms.py
class UserForm(forms.ModelForm):
class Meta:
model=User
fields=['username','email','password']
# ...

In above code you, Meta is a subclass of the UserForm class and is used to provide metadata to the UserForm class.

Specifically, Meta is used to define the model class (User) and the fields that should be included in the form (username, email, and password). When the form is rendered, it will include HTML input elements for each of these fields.

note

The Meta subclass can also be used to provide additional information about the form, such as custom validation logic, field ordering, or help text for individual fields.

Login Form

Login form will be related to User model. Let's have a look into the code.

blog/forms.py
class LoginForm(forms.ModelForm):
class Meta:
model=User
fields=['username','password']

Blog Form

Blog form will be related to Blog model. Let's have a look into the code.

blog/forms.py
class BlogForm(forms.ModelForm):
class Meta:
model=Blog
fields=['title','content','image']

Comment Form

Comment form will be related to Comment model. Let's have a look into the code.

blog/forms.py
class CommentForm(forms.ModelForm):
# add new field to the form named id
class Meta:
model=Comment
fields=['comment']

Final File

After writing above code, forms.py should look like following

blog/forms.py
from django import forms
from .models import *
from django.contrib.auth.models import User
class UserForm(forms.ModelForm):
class Meta:
model=User
fields=['username','email','password']
class LoginForm(forms.ModelForm):
class Meta:
model=User
fields=['username','password']

class BlogForm(forms.ModelForm):
class Meta:
model=Blog
fields=['title','content','image']

class CommentForm(forms.ModelForm):
# add new field to the form named id
id = forms.IntegerField(required=True, widget=forms.HiddenInput())
class Meta:
model=Comment
fields=['comment', 'id']