How to Create Cascading/Dependent Dropdown List in Django python

In this tutorial we will learn how to create cascading dropdown list in django python. In this small application we are going to  improve user experience  by  Creating dynamic dropdown  list using jquery.


<!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">
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>

  <div class="container">
  <div class="card body">
    <div class="row">
    <div class="col-md-4">
        <select name="dept" id="department" class="form-control">       
            <option value="0">--Select--</option>     
            {% for item in department  %}
            <option value="{{ }}">{{ item.departmentname }}</option>
            {% endfor %}
    <div class="col-md-4">
        <select name="emp" id="Employee" class="form-control">
            <option value="0">--Select--</option>  
            {% for item in employee  %}
            <option value="{{ item.deptid_id }}">{{ item.empname }}</option>
            {% endfor %}

<script src="/Dropdownjs"></script>



 var department = $("#department");
 var Employee = $("#Employee");
 var $options = Employee.find('option');
    Employee.html($options.filter('[value="'+ this.value  +'"]'));


from django.urls.conf import path
from dropdown import  views 

urlpatterns = [

from django.db import models
from django.db.models.base import Model
from django.db.models.deletion import CASCADE

# Create your models here.

class department(models.Model):
    departmentname = models.CharField(max_length=100)

class employee(models.Model):
    deptid = models.ForeignKey(department,on_delete=CASCADE)
    empname = models.CharField(max_length=100)
    age = models.IntegerField()
    address = models.CharField(max_length=500, null=True)
    contactno = models.CharField(max_length=20)

from django.shortcuts import render
from .models import department,employee

# Create your views here.

def getdata(request):
    template_name = 'dropdown.html'
    deptcontext = department.objects.all()
    empcontext = employee.objects.all()

    return render(request,template_name,{'department':deptcontext,'employee':empcontext})

Please leave your  comments and suggestion

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments