title: How can I fix error "'NoneType' object has no attribute 'day'" in django website
body: <p>I noticed this error in my application, &quot;'NoneType' object has no attribute 'day'&quot;. What I have noticed about it is that. I have a model named course_schedule, the course schedule has an option of Monday to Sunday. If the course schedule is partially populated, that is I populate only some days, like 3 days out of the complete 7 days in a week, I get the error but whenever I populate the course schedule populate course schedule model completely, I don't have the error and everything works well.</p>
  <p>error log:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\Habib\Documents\django\FIVERR\Ayyub_SMS\venv\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;C:\Users\Habib\Documents\django\FIVERR\Ayyub_SMS\venv\lib\site-packages\django\core\handlers\base.py&quot;, line 181, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;C:\Users\Habib\Documents\django\FIVERR\Ayyub_SMS\sms\schoolapp\views.py&quot;, line 1609, in student_course_list
      get_all = getall(2021, sch.day)
  
  Exception Type: AttributeError at /en/student_course_list
  Exception Value: 'NoneType' object has no attribute 'day'
  </code></pre>
  <p>models.py</p>
  <pre><code>class course_schedule(models.Model):
  
      days_choices = (
          (&quot;Mon&quot;, 'Monday'),
          (&quot;Tue&quot;, 'Tuesday'),
          ('Wed', &quot;Wednessday&quot;),
          ('Thurs', &quot;Thursday&quot;),
          ('Fri', &quot;Friday&quot;),
          ('Sat', &quot;Saturday&quot;),
          (&quot;Sun&quot;, &quot;Sunday&quot;),
      )
  
      day = models.CharField(max_length=60, default=&quot;Sun&quot;)
      time_session = models.CharField(max_length=150, default='8:00am')
      end_session = models.CharField(max_length=150, default='10:00am')
  
      def __str__(self):
          return self.day
  
  class add_courses(models.Model):
      Course_Name = models.CharField(max_length=200, blank=True)
      Manager_Name = models.ForeignKey(Manager_login_information, on_delete=models.CASCADE, blank=True)
      description = models.TextField(default='', blank=True)
      syllabus = models.TextField(default='', blank=True)
      student = models.ManyToManyField(add_students_by_manager, blank=True)
      schedule = models.ForeignKey(course_schedule, on_delete=models.CASCADE, blank=True)
  
      def __str__(self):
          return self.Course_Name
  </code></pre>
  <p>views.py</p>
  <pre><code>def getall(year, day):
      dates = {
          &quot;Sun&quot;: 6,
          &quot;Mon&quot;: 0,
          &quot;Tue&quot;: 1,
          &quot;Wed&quot;: 2,
          &quot;Thurs&quot;: 3,
          &quot;Fri&quot;: 4,
          &quot;Sat&quot;: 5
      }
      d = date(year, 1, 1)                    # January 1st
      d += timedelta(days = dates[day] - d.weekday())  # First day
      while d.year == year:
          yield d
          d += timedelta(days = 7)
  
  def student_course_list(request):
  
      stu_course_id = request.POST.get('stu_course_id')
  
      stdnt = add_students_by_manager.objects.get(student_ID=request.session['Stu_id'])
      my_announcement = Announcement_by_dean.objects.filter(student_id=stdnt.pk).order_by('-id')
      print(my_announcement)
  
      if request.method==&quot;POST&quot;:
          id_name = student_usid+' - '+student_usname
          courses = add_courses.objects.filter(student=stu_course_id_get)
          days = {}
          for course in courses:
              sch = course_schedule.objects.filter(id=course.pk).first()
  
              get_all = getall(2021, sch.day)
  
              lists = {}
              for n in get_all:
  
                  date = n.strftime(&quot;%d/%m/%Y&quot;)
  
                  try:
                      if days[str(date)]:
  
                          days[str(date)].append(str(course.Course_Name)+&quot; &quot;+str(course.course_code))
  
                  except KeyError:
  
                      days[str(date)] = [str(course.Course_Name)+&quot; &quot;+str(course.course_code),]
  
          # print(days)
  
          context = {'days': days, 'courses':courses, 'my_announcement':my_announcement}
          return render(request, 'student_course_list.html', context)
      else:
          return redirect('/')
  </code></pre>