Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What is ChoiceField in Django Forms?

Njoku Ifeanyi Gerald

Django is an open-source, high-level Python web framework that encourages rapid development and clean, pragmatic design.

ChoiceField in Django Forms is a string field for selecting a particular choice out of a list of available choices. ChoiceField can be used in cases where there are a limited number of pre-defined options to select from, e.g., selecting a country from a list of countries or a day from among the seven days of the week.

ChoiceField is similar to an option field in web pages, where someone can select an option of their choice from a list of options.

ChoiceField uses an iterable of a 2-tuple to add as choices.


A basic implementation of a ChoiceField is shown below:

class FRUIT(forms.Form):
      (1, 'Orange'),
      (2, 'Mango'),
      (3, 'Strawberries'),
      (4, 'Grapes'),
  name = forms.CharField()
  category = forms.ChoiceField(choices=CHOICES)

In the code above, the fruit names are the choices that can be chosen from.

A complete demonstration of how the choices would be part of the application is provided below.

Let’s start with installation:

pip install pipenv
pipenv shell
pipenv install django


django-admin startproject DjangoChoiceField ./
python startapp codebase
python migrate
python runserver

Next, add the code below to



In the codebase app folder, go to and use ChoiceField to make the options for the form.

from django import forms

#The choices to choose from
    ( "BCH","biochemistry"),
    ( "EE","electrical engineering"),
    ( "BTC", "biotechnology"),
    ( "BIO","biology"),
    ( "CE", "chemical engineering"),
    ( "OPT", "Optometry"),
    ( "SE", "software engineering"),
    ( "PUT", "public Health"),
    ("STA", "statistics" ),
# creating a form 
class ChoiceForm(forms.Form):
    departments  = forms.ChoiceField(choices = DEPARTMENTS)

In, a view for the web page itself is created as follows.

from django.shortcuts import render
from .forms import  ChoiceForm
# Create your views here.
def home(request):
    form = ChoiceForm()
    context = {
        'form': form
    return render( request,"app/home.html", context)

In the codebase app, create a folder and name it “templates.” Inside the “templates” folder, create another folder and name it “app.” Then, inside the “app” folder, create the home.html file, which displays a web page that contains the form and a button to submit it.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    Different FUTO departments
    <form method = "GET">
        {{ form }}
        <input type = "submit" value = "Submit">

In the “DjangoChoiceField” folder, the following is added to

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    # including the codebase file
    path('', include('codebase.urls'))

In the codebase folder, create a file and name it with the content below to route the web pages across the application.

from django.urls import path
#import the home function from the file
from .views import home

urlpatterns = [
    #creating a route
    path('', home, name='home'),

Finally, to run the application and interact with the form, run the following commands:

python makemigrations

python migrate
python runserver

The result can be viewed on the localhost ( to access the app.




Njoku Ifeanyi Gerald

View all Courses

Keep Exploring