0

I have created a template that renders a dropdown list of students.

I want to filter a query set on another view to show schedule info for just the student that was selected.

perm_id in student_list is primary key and perm in StudentSchedule is foreign key.

Here are snippets of my models.py, forms.py and views.py

models.py

class StudentList(models.Model):
    student_name = models.CharField(max_length=45, blank=True, null=True)
    perm_id = models.IntegerField(primary_key=True)

class StudentSchedule(models.Model):
    perm = models.ForeignKey(StudentList, on_delete=models.CASCADE)
    begin_period = models.CharField(max_length=45, blank=True, null=True)
    section_id = models.CharField(max_length=45, blank=True, null=True)
    course_title = models.CharField(max_length=45, blank=True, null=True)
    last_name1 = models.CharField(max_length=45, blank=True, null=True)

forms.py

class StudentForm(forms.Form):
    Student = forms.ModelChoiceField(queryset=StudentList.objects.all()
                                .order_by('student_name'))

views.py

class ScheduleView(View):
   form_class = ScheduleForm
   template_name = 'learning_logs/student_schedule.html'

   def get(self, request):
       form = self.form_class(initial={})
       data = StudentSchedule.objects.filter()
       return render(request, self.template_name, {'form': form, 'data': 
data})

I can do the following and get student with id # 123456

data = StudentSchedule.objects.filter(perm=123456)

But, I want Django to use the perm_id from the student selected from StudentList

My urls are as follows:

# Page for selecting a student from drop down list
path('select_student/', views.StudentView.as_view(), name='select_student'),

# Page for displaying student schedule
path('student_schedule/<int:pk>/$', views.ScheduleView.as_view(), 
name='student_schedule'),
DFenstermacher
  • 564
  • 1
  • 9
  • 23

1 Answers1

0

Say your path is like

urlpatterns = [
    path('students/<int:pk>/$', views.ScheduleView.as_view(), name='student-detail'),
    ...
]

Then you will get a additional argument in your get method and you can access that as

class ScheduleView(View):
   form_class = ScheduleForm
   template_name = 'learning_logs/student_schedule.html'

   def get(self, request, pk):
       form = self.form_class(initial={})
       data = StudentSchedule.objects.filter(perm=pk)
       return render(request, self.template_name, {'form': form, 'data': 
data})

Edit

And also in your student_schedule.html you need to perm.id to access this url as

{% url ...  perm.id%}
Shakil
  • 4,520
  • 3
  • 26
  • 36
  • Did as you suggested and received the following error: – Scott Norman Jan 22 '19 at 00:07
  • Reverse for 'student_schedule' with no arguments not found. 1 pattern(s) tried: ['student_schedule/(?P[0-9]+)/\\$$'] Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 2.1.5 Exception Type: NoReverseMatch Exception Value: Reverse for 'student_schedule' with no arguments not found. 1 pattern(s) tried: ['student_schedule/(?P[0-9]+)/\\$$'] – Scott Norman Jan 22 '19 at 00:07
  • @ScottNorman you need to add prem.id in your accessing url. I have updated my response. – Shakil Jan 22 '19 at 03:58