Forms with branching/filtering

Hello,
I’m working on a gradebook app. Some pseudocode for my models looks like:

class Course
    name

class Section
    name
    course = models.ForeignKey(Course)

class Student
    name
    section = models.ForeignKey(Section)
    grade

If I want to enter in a grade for students, first I would select the course, then the section in the course, and I would then get a list of students where I can now enter in the grade. I can only think of two ways to do this.

  1. HTML only, use separate views/forms/templates for each step. template/Form1 selects the course and I redirected a new page/template/form with a choice of sections. Select the section I want and then I’m directed to a template/view/form where I enter in the grade.
  2. Have one template/view/form but use some javascript to hide parts of the form based on selections. Ex. The form would have all the courses, sections and students but the sections and students are hidding. Selecting a course button reveals the relevent sections and selecting section buttons reveals students. I don’t know if this actually works because you’d end up with most students having empty data in the form. Maybe some javascript can make an empty box be ignored or something like that.

I also guess that method #2 is not very efficient. While many teachers would have a maximum of 200 students, there could be a teacher that is assigned to grade level courses and end up with maybe 2000 students.

I think I need to do this type of task in many scenarios with this app. I also think this must be a very common problem in working with django and other web apps, where the user has to weave down through a series of selections.

I’m hoping someone can offer some insight into strategies for accomplishing this. Thanks.

Hi @shmish.

It really is. :sweat_smile:

For #2, I’d go with this django-select2, specifically with this example.
It relies on select2, which is a JS library that allows this kind of chained select and it’s integrated with Django.

1 Like

Thanks @marcorichetta
Do you know if django-select2 strictly requires Redis? It looks like this is a requirement and that I would have to set up a redis server on a linux machine. Currently I’m working with a Windows PC.

No, django-select2 does not require Redis. What it requires is a persistent cache backend be used. See Django-Select2 — Django-Select2 7.4.2 documentation

1 Like