In Django, I would like to narrow a choice using a form to another form

I am trying to use the result of a selection as input to another selection. I have been working on this for three weeks, and I cannot find a simple answer.

Here are my models:

from django.db import models

# Create your models here.
class School(models.Model):

        id = models.CharField(primary_key=True, max_length=10)
        school_name = models.CharField(max_length=80)

        def __str__(self):
                return(f"{self.id} {self.school_name}")
                return self.id

        class Meta:
                ordering = ['id']
                indexes = [
                models.Index(fields=['id', 'school_name']),
                ]

class Courses(models.Model):

        id = models.CharField(primary_key=True, max_length=12)
        created_at = models.DateTimeField(auto_now_add=True)
        school = models.ForeignKey(School,  on_delete=models.PROTECT, related_name='school')
        name = models.CharField(max_length=150, null=True, blank=True)
        cost = models.CharField(max_length=75, null=True, blank=True)

        class Meta:
            ordering = ['id']
            indexes = [ models.Index(fields=['school']), models.Index(fields=['name', 'id'])]

        def __str__(self):
            return(f"{self.school} {self.name}")
            return self.name

class Student(models.Model):

        course = models.ForeignKey(Courses,  on_delete=models.PROTECT)
        created_at = models.DateTimeField(auto_now_add=True)
        student_id = models.CharField(max_length=20, null=True, blank=True)

class Meta:
        ordering = ['course']
        indexes = [
        models.Index(fields=['course']),
             
        ]

And my views:

def choose_school3(request):
    
    if request.user.is_authenticated:

        aschool = Courses.objects.all()
        form=SelectSchoolForm(request.POST)
        if request.method == "POST":
            if form.is_valid():
                data = form.cleaned_data
                choice = data['school']
                selected_school = Courses.objects.filter(school=choice).values()
    
                context = {'choice': choice}
                return render(request, 'add_student2b.html', context)
        context = {'form': form, 'aschool':aschool}  
        return render(request, 'choose_school3.html', context)  
    else:
        messages.success(request, "You Must Be Logged In To View Records!")
        return redirect('main')
def add_student2b(request, choice):

        chosen_school = Courses.objects.filter(school=choice).values()

        form = AddStudentForm(request.POST)
    
        context = {'form':form, 'choice':choice, 'chosen_school':chosen_school}
        return render(request, 'add_student2b.html', context)
def add_student5(request, pk):

    if request.user.is_authenticated:
        choice = School.objects.get(id=pk)
        courses = Courses.objects.filter(school=choice)
        
        form = modelformset_factory(Student, fields=['course','student_id'], formset=AddStudentForm2FormSet)

        context = {'courses':courses, 'choice':choice, 'form':form}
    
        return render(request, 'add_student5.html', context)

    else:
        messages.success(request, "You Must Be Logged In To View Records!")
        return redirect('main')

The forms:

class SelectSchoolForm(forms.ModelForm):

    class Meta:
            model = Courses
            fields = ('school', )
            labels = {'school':'School', }
            widgets = {'school': forms.Select(attrs={"placeholder":"School", "class":"form-select"}),

             }
class  AddStudentForm(forms.ModelForm):


    class Meta:
                model = Student
                fields = ( 'student_id', )
                labels = {'student_id':'',}
                widgets = {'student_id':forms.widgets.TextInput(attrs={"placeholder":"Student ID", "class":"form-control"}),
               
                }

class AddStudentForm2FormSet(BaseModelFormSet):


            def __init__(self, *args, **kwargs):

                super().__init__(self, *args, **kwargs)
                self.fields['course'].queryset = Student.objects.filter(all)

and html:
choose_school3.html:

<form class="row g-3" action="{% url 'choose_school3' %}" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
<div class="col-md-6">
    
    
    {{ form.as_p }}
    
</div>

<div class="col-md-6">
<button type="submit" class="btn btn-secondary">Choose School</button>

<a href="{% url 'school' %}" class="btn btn-success">Back</a>
</div>

</form>

add_student2b.html:

<form class="row g-3" action="{% url 'choose_school' %}" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
<div class="col-md-6">
    
    
    {{ form.as_p }}
    
</div>

<div class="col-md-6">

<a class="btn btn-secondary btn-lg" href="{% url 'add_student5' choice.id  %}" role="button">Add Student</a>

<a href="{% url 'school' %}" class="btn btn-success">Back</a>
</div>

</form>

and add_student5.html:

<form class="row g-3" action="{% url 'choose_school' %}" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
<div class="col-md-6">
    
    
    {{ form.as_p }}
    
</div>

<div class="col-md-6">

<a class="btn btn-secondary btn-lg" href="{% url 'add_student5' choice.id  %}" role="button">Add Student</a>

<a href="{% url 'school' %}" class="btn btn-success">Back</a>
</div>

</form>

Everything I try sends me to an error. This most recent incarnation gives me this:

cannot unpack non-iterable builtin_function_or_method object

referring to the {{ form.as_p }} tag. I’ve been going in circles and down rabbit holes. Is it possible to do this with Django (and without JS or other things) or not? Please help.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật