django
  1. django-djangoforms

Django Forms - Django Fundamentals

Introduction

Django Forms is a module in Django that allows you to generate HTML forms and validate user inputs. It makes it easy to create forms in a consistent manner, regardless of how complex they are.

Syntax

from django import forms

class MyForm(forms.Form):
    my_field = forms.CharField(label='My Label')

Example

To create a simple form with a single text input field, add the following code to your Django view:

from django.shortcuts import render
from django import forms

class NameForm(forms.Form):
    your_name = forms.CharField(label='Your name', max_length=100)

def get_name(request):
    if request.method == 'POST':
        form = NameForm(request.POST)
        if form.is_valid():
            your_name = form.cleaned_data['your_name']
            return HttpResponse('Hello, ' + your_name)
    else:
        form = NameForm()

    return render(request, 'name.html', {'form': form})

In this example, the NameForm class is used to define the form. It has one field, your_name, which is a CharField object. When the get_name function is called, it checks whether the request method was POST or GET. If it was POST, it creates an instance of the NameForm class with the POST data provided by the user. It then checks whether the form is valid using the is_valid() method. If the form is valid, it retrieves the value of the your_name field using the cleaned_data dictionary. If the request was a GET request, it simply creates a new instance of the NameForm class.

Output

Here is an example output of the above code when submitting the form with the name "john":

Hello, john

Explanation

In the above example, the NameForm class is used to define the form. It has one field, your_name, which is a CharField object. When the get_name function is called, it checks whether the request method was POST or GET. If it was POST, it creates an instance of the NameForm class with the POST data provided by the user. It then checks whether the form is valid using the is_valid() method. If the form is valid, it retrieves the value of the your_name field using the cleaned_data dictionary. If the request was a GET request, it simply creates a new instance of the NameForm class.

Use

Django Forms is useful when you need to create forms in your Django application. It can be used to create simple or complex forms, depending on your needs.

Important Points

  • Form classes inherit from django.forms.Form, not django.forms.ModelForm.
  • Form fields don’t require a corresponding database column, as they only exist virtually on the form instance.
  • Form fields can have different validation rules applied to them using validators.
  • Forms currently only support POST requests.

Summary

  • Django Forms is a built-in module in Django that allows you to generate HTML forms and validate user inputs.
  • Form classes inherit from django.forms.Form and can be used to define simple or complex forms.
  • Forms require validation, which can be done using the is_valid() method.
  • When the form is valid, the values of the form fields can be accessed using the cleaned_data dictionary.
Published on: