title: Django Index Error: List Index out of Range using try catch
body: <p>How do I throw in a try catch exception if there is no data within field issuedbook? I haven't put any data inside issuedbook for debugging purposes and I just want it to say &quot;No Data found&quot; or &quot;You have no books issued&quot;</p>
  <p>Here is my views.py:</p>
  <pre class="lang-python prettyprint-override"><code>def viewissuedbookbystudent(request):
          student=models.StudentExtra.objects.filter(user_id=request.user.id)
          issuedbook=models.IssuedBook.objects.filter(enrollment=student[0].enrollment)
  
          li1=[]
          li2=[]
  
          for ib in issuedbook:
              books=models.Book.objects.filter(isbn=ib.isbn)
              for book in books:
                  t=(request.user,student[0].enrollment,student[0].branch,book.name,book.author)
                  li1.append(t)
              issdate=str(ib.issuedate.day)+'-'+str(ib.issuedate.month)+'-'+str(ib.issuedate.year)
              expdate=str(ib.expirydate.day)+'-'+str(ib.expirydate.month)+'-'+str(ib.expirydate.year)
              #fine calculation
              days=(date.today()-ib.issuedate)
              print(date.today())
              d=days.days
              fine=0
              if d&gt;15:
                  day=d-15
                  fine=day*10
              t=(issdate,expdate,fine)
              li2.append(t)
          context={'li1':li1,'li2':li2}
          return render(request,'library/viewissuedbookbystudent.html',context)
  </code></pre>
  <p>I get this error every time I run it and I know it's because of no data within issuedbook field:</p>
  
  <pre><code>IndexError at /viewissuedbookbystudent
  
  list index out of range
  
  Request Method:     GET
  Request URL:    http://127.0.0.1:8000/viewissuedbookbystudent
  Django Version:     3.2.4
  Exception Type:     IndexError
  Exception Value:    
  
  list index out of range
  
  Exception Location:     C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py, line 318, in __getitem__
  Python Executable:  C:\Users\Admin\AppData\Local\Programs\Python\Python39\python.exe
  Python Version:     3.9.4
  Python Path:    
  
  ['E:\\C drive\\Users\\Admin\\PycharmProjects\\librarymanagement-master',
   'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
   'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
   'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\lib',
   'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39',
   'C:\\Users\\Admin\\AppData\\Roaming\\Python\\Python39\\site-packages',
   'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages'
  </code></pre>
  <p>Here is my template for this, if it helps:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="row"&gt;
  
    &lt;div class="col-lg-6" style="padding-right:0px;"&gt;
      {% if li1%}
      &lt;table class="redTable"&gt;
    &lt;thead&gt;
    &lt;tr&gt;
    &lt;th&gt;Name&lt;/th&gt;
    &lt;th&gt;Enrollment&lt;/th&gt;
    &lt;th&gt;Branch&lt;/th&gt;
      &lt;th&gt;Book Title&lt;/th&gt;
        &lt;th&gt;Book Author&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tfoot&gt;
    &lt;tr&gt;
    &lt;td colspan="5"&gt;
    &lt;div class="links"&gt;&lt;/div&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tfoot&gt;
    &lt;tbody&gt;
      {% for t in li1 %}
  &lt;tr&gt;
    &lt;td&gt;  {{t.0}}&lt;/td&gt;
    &lt;td&gt;  {{t.1}}&lt;/td&gt;
    &lt;td&gt;  {{t.2}}&lt;/td&gt;
    &lt;td&gt;  {{t.3}}&lt;/td&gt;
    &lt;td&gt;  {{t.4}}&lt;/td&gt;
  &lt;/tr&gt;
  
      {% endfor %}
    &lt;/tbody&gt;
    &lt;/table&gt;
  
  &lt;/div&gt;
  
  &lt;div class="col-lg-6" style="padding-left:0px;"&gt;
  
        &lt;table class="redTable"&gt;
      &lt;thead&gt;
      &lt;tr&gt;
      &lt;th&gt;Issue Date&lt;/th&gt;
      &lt;th&gt;Expiry Date&lt;/th&gt;
      &lt;th&gt;Fine&lt;/th&gt;
      &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tfoot&gt;
      &lt;tr&gt;
      &lt;td colspan="5"&gt;
      &lt;div class="links"&gt;&lt;/div&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
      &lt;/tfoot&gt;
      &lt;tbody&gt;
        {% for t in li2 %}
  &lt;tr&gt;
    &lt;td&gt;  {{t.0}}&lt;/td&gt;
    &lt;td&gt;  {{t.1}}&lt;/td&gt;
    &lt;td&gt;  {{t.2}}&lt;/td&gt;
  &lt;/tr&gt;
        {% endfor %}
      &lt;/tbody&gt;
      &lt;/table&gt;
      {%else%}
      &lt;h1&gt; No book Issued to you&lt;/h1&gt;
      {%endif%}
  
  &lt;/div&gt;
  
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  
  This is the models.py:</p>
  
  <pre><code>class StudentExtra(models.Model):
      user=models.OneToOneField(User,on_delete=models.CASCADE)
      enrollment = models.CharField(max_length=40,verbose_name=_('Student ID'))
      course = models.CharField(max_length=40)
      #used in issue book
      def __str__(self):
          return self.user.last_name+'['+str(self.enrollment)+']'
      @property
      def get_name(self):
          return self.user.last_name
      @property
      def getuserid(self):
          return self.user.id
  
  class Category(models.Model):
      class Meta:
          verbose_name = _('Category')
          verbose_name_plural = _('Categories')
          ordering = ['id']
  
      name = models.CharField(max_length=255, verbose_name=_('Category'))
  
      def __str__(self):
          return self.name
  
  class Book(models.Model):
      class Meta:
          verbose_name = _('Book')
          verbose_name_plural = _('Books')
          ordering = ['id']
  
      title=models.CharField(max_length=130)
      isbn=models.PositiveIntegerField()
      author=models.CharField(max_length=140)
      category=models.ForeignKey(Category,on_delete=models.CASCADE)
      def __str__(self):
          return str(self.title)+&quot;[&quot;+str(self.isbn)+']'
  
  
  def get_expiry():
      return datetime.today() + timedelta(days=15)
  
  class IssuedBook(models.Model):
      #moved this in forms.py
      #enrollment=[(student.enrollment,str(student.get_name)+' ['+str(student.enrollment)+']') for student in StudentExtra.objects.all()]
      enrollment=models.CharField(max_length=30)
      #isbn=[(str(book.isbn),book.name+' ['+str(book.isbn)+']') for book in Book.objects.all()]
      isbn=models.CharField(max_length=30)
      issuedate=models.DateField(auto_now=True)
      expirydate=models.DateField(default=get_expiry)
      def __str__(self):
          return self.enrollment
  </code></pre>
  <p>I have Traceback as well:
  <a href="https://i.stack.imgur.com/wrxgg.png" rel="nofollow noreferrer">Traceback.png</a></p>
  <p>Can I fix it using a try catch? If yes, how?</p>