O'Reilly logo

Django Project Blueprints by Asad Jibran Ahmed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Looking at a Django form

As our aim is to allow users to create dynamic forms based on parameters stored in a database, a good place to start is to look at how a Django form works under the hood and what options we have to customize it. First, let's create a basic form. Create a new main/forms.py file and add the following code to it:

from django import forms

class SampleForm(forms.Form):
    name = forms.CharField()
    age = forms.IntegerField()
    address = forms.CharField(required=False)
    gender = forms.ChoiceField(choices=(('M', 'Male'), ('F', 'Female')))

This is a pretty basic form. However, it has a variety of form fields that we can look at. Let's play around a bit in the shell, which you can start as follows:

> python manage.py shell

Tip

I usually like ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required