title: RelatedObjectDoesNotExist at /teacher/class_students_list
body: <p>Views.py</p>
  <pre><code>#For Student Registration 
  def StudentSignUp(request):
      user_type = 'Student'
      registered= False
  
      if request.method == &quot;POST&quot;:
          logger_Form = loggerform(data = request.POST)
          Student_Form = StudentForm(data = request.POST)
          if logger_Form.is_valid() and Student_Form.is_valid():
  
              user = logger_Form.save()
              user.is_student = True
              user.save()
  
              profile = Student_Form.save(commit=False)
              profile.user = user
              profile.save()
  
              registered = True
          else:
              print(logger_Form.errors,Student_Form.errors)
      else:
          logger_Form = loggerform()
          Student_Form = StudentForm()
      return render(request,'student_signup.html',{'logger_Form':loggerform,'Student_Form':StudentForm,'registered':registered,'user_type':user_type})
  
  # FOR teacher Registration 
  def TeacherSignUp(request):
      user_type = 'Teacher'
      registered = False 
  
      if request.method == &quot;POST&quot;:
          logger_Form = loggerform(data = request.POST)
          Teacher_Form = TeacherForm(data = request.POST)
  
          if logger_Form.is_valid() and Teacher_Form.is_valid():
  
              user = logger_Form.save()
              user.is_teacher = True
              user.save()
  
              profile = Teacher_Form.save(commit=False)
              profile.user = user
              profile.save()
  
              registered = True
          else:
              print(logger_Form.errors,Teacher_Form.errors)
      else:
          logger_Form = loggerform()
          Teacher_Form = TeacherForm()
      return render(request,'teach_signup.html',{'logger_Form':loggerform,'Teacher_Form':TeacherForm,'registered':registered,'user_type':user_type})
  
  
  #List of all the students that teacher has added in their class
  def class_students_list(request):
      query = request.GET.get(&quot;q&quot;, None)
      students = StudentsInClass.objects.filter(teacher = request.user.Teacher)
      students_list = [x.student for x in students]
      qs = Student.objects.all()
      if query is not None:
          qs = qs.filter(
                  Q(name__icontains=query)
                  )
      qs_one = []
      for x in qs:
          if x in students_list:
              qs_one.append(x)
          else:
              pass
      context = {
          &quot;class_students_list&quot;: qs_one,
      }
      template = &quot;class_students_list.html&quot;
      return render(request, template, context)
  
  class ClassStudentsListView(LoginRequiredMixin,DetailView):
      model = models.Teacher
      template_name = &quot;class_students_list.html&quot;
      context_object_name = &quot;teacher&quot;
  </code></pre>
  <p>Models.py</p>
  <pre><code>class logger(AbstractUser):
      is_student = models.BooleanField(default=False)
      is_teacher = models.BooleanField(default=False)
  
  class Student(models.Model):
      username = models.OneToOneField(logger,on_delete=models.CASCADE,primary_key=True,related_name='Student')
      name=models.CharField(max_length=250)
      roll_no = models.CharField(max_length=50)
      email = models.EmailField(max_length=254)
  
      def __str__(self):
          return self.name
  
      class Meta:
          ordering = ['roll_no']
  
  class Teacher(models.Model):
      username = models.OneToOneField(logger,on_delete=models.CASCADE,primary_key=True,related_name='Teacher')
      name = models.CharField(max_length=250)
      subject_name = models.CharField(max_length=250)
      email = models.EmailField(max_length=254)
      class_students = models.ManyToManyField(Student,through=&quot;StudentsInClass&quot;)
  
      def __str__(self):
          return self.name
      
      def get_absolute_url(self):
          return reverse('teacher_detail',kwargs={'pk':self.pk})
  
  class StudentsInClass(models.Model):
      teacher = models.ForeignKey(Teacher,related_name=&quot;class_teacher&quot;,on_delete=models.CASCADE)
      student = models.ForeignKey(Student,related_name=&quot;user_student_name&quot;,on_delete=models.CASCADE)
  
      def __str__(self):
          return self.student.name
  
      class Meta:
          unique_together = ('teacher','student')
  </code></pre>
  <p>forms.py</p>
  <pre><code># User Login Form common for both student and teacher login
  class loggerform(UserCreationForm):
      class Meta():
          model = logger
          fields = ['username','password1','password2']
          widgets = {
                  'username': forms.TextInput(attrs={'class':'answer'}),
                  'password1': forms.PasswordInput(attrs={'class':'answer'}),
                  'password2': forms.PasswordInput(attrs={'class':'answer'}),
                  }
  
  # Teacher SignUp Form 
  class TeacherForm(forms.ModelForm):
      class Meta():
          model =  Teacher
          fields = ['name','subject_name','email']
          widgets = {
                  'name': forms.TextInput(attrs={'class':'answer'}),
                  'subject_name': forms.TextInput(attrs={'class':'answer'}),
                  'email': forms.EmailInput(attrs={'class':'answer'}),
                  }
  
  # Student SignUp Form
  class StudentForm(forms.ModelForm):
      class Meta():
          model =  Student
          fields = ['name','roll_no','email']
          widgets = {
                  'name': forms.TextInput(attrs={'class':'answer'}),
                  'roll_no': forms.NumberInput(attrs={'class':'answer'}),
                  'email': forms.EmailInput(attrs={'class':'answer'}),
                  }
  </code></pre>
  <p>class_students_list.html</p>
  <pre><code>{% extends &quot;base.html&quot; %}
  {% load static %}
  {% block content %}
  
  &lt;div class=&quot;student-list&quot;&gt;
  &lt;div class=&quot;container&quot;&gt;
    {% if class_students_list.count == 0 %}
      &lt;h1&gt;No students in the class.&lt;/h1&gt;
      {% else %}
  
    &lt;form method='GET' action=&quot;{% url 'class_student_list' %}&quot;&gt;
      &lt;p&gt;Search Student: &lt;input type='search' name='q' placeholder='Search'&gt;&lt;/p&gt;
    &lt;/form&gt;
    &lt;div class=&quot;row&quot;&gt;
    {% for student in class_students_list %}
    &lt;div class=&quot;col-6 col-md-4&quot;&gt;
    &lt;div class=&quot;student-box&quot;&gt;
      &lt;div class=&quot;imgBx&quot;&gt;
        {% if student.student_profile_pic %}
        {% else %}
        {% if forloop.counter0|divisibleby:&quot;2&quot; %}
        {% else %}
        {% endif %}
        {% endif %}
      &lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;
        &lt;h3&gt;{{ student.name }}&lt;/h3&gt;
        &lt;p&gt;&lt;i class=&quot;far fa-user&quot;&gt;&lt;/i&gt; Username: {{ student.user.username }}&lt;/p&gt;
        &lt;p&gt;&lt;i class=&quot;fal fa-user-tag&quot;&gt;&lt;/i&gt; Roll No.: {{ student.roll_no }}&lt;/p&gt;
        &lt;p&gt;&lt;i class=&quot;far fa-phone&quot;&gt;&lt;/i&gt; Phone: {{ student.phone }}&lt;/p&gt;
        &lt;p&gt;&lt;i class=&quot;far fa-envelope-open-text&quot;&gt;&lt;/i&gt; Email ID: {{ student.email }}&lt;/p&gt;
        &lt;a href=&quot;#&quot;&gt;&lt;p class=&quot;add-button&quot;&gt;View Marks Profile&lt;/p&gt;&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    {% endfor %}
    &lt;/div&gt;
  &lt;/div&gt;
  {% endif %}
  &lt;/div&gt;
  
  {% endblock %}
  </code></pre>
  <p>This is the Traceback that I'm getting:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py&quot;, line 179, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;/Users/vinayak/Desktop/CS IA /src/IB_Portal/Portal/views.py&quot;, line 125, in class_students_list
      students = StudentsInClass.objects.filter(teacher = request.user.Teacher)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/utils/functional.py&quot;, line 241, in inner
      return func(self._wrapped, *args)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py&quot;, line 421, in __get__
      raise self.RelatedObjectDoesNotExist(
  Portal.models.logger.Teacher.RelatedObjectDoesNotExist: logger has no Teacher.
  [28/Feb/2021 10:02:23] &quot;GET /teacher/class_students_list HTTP/1.1&quot; 500 67048
  </code></pre>
  <p>I have posted my code, above I dont know why I keep getting the error in the title, I can't seem to figure it out, can anyone please help me out, I'm still in school and new to django. Any sort of help would be appreciated. Anyone who can solve the problem, please post the solution. Thank you.</p>