Entries not showing up in the django table

Viewed 515

Problem: I have got one contact model. In this contact model, I`ve got severals type (supplier, Customer and Employee). The data has been uploaded in the SQL table but it is not showing up in Django/html table. Do I need to change the way how my model is written?

Thanks in advance

Model:

class Contact(models.Model):

    CONTACT_TYPE = (      
        (0, 'Customer'),
        (1, 'Supplier'),
        (2, 'Employee'),
      
        )
    GENDER = (      
        (0, 'Male'),
        (1, 'Female'),
        )
    

    type = models.IntegerField(choices=CONTACT_TYPE,default=1)
    gender = models.IntegerField(choices=GENDER,default=0)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, blank=True, null=True)

Views:

def index_supplier(request):
    contacts = Contact.objects.all()
    suppliers = Contact.objects.filter(type=1).all()
    count_supplier= Contact.objects.filter(type=1).count()        
    context = {
                'contacts': contacts,
                'count_supplier': count_supplier,                   
                         
            }
    return render(request, 'contacts/index_supplier.html', context)

Templates:

<div class="container-fluid">
     <div class="col-sm-20">
           <table id="dtBasicExample" class="table table-striped table-hover">
           <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
           <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
           <script>
                   $(document).ready(function(){
                   $('#dtBasicExample').DataTable();
            });
           </script>                    
           <thead>
               <tr>
                    <th>Type</th> 
                    <th>Contact Name</th>
                    <th>Company</th>                             
               </tr>
           </thead>
           <tbody>
               {% for contact in suppliers.all %}
               <tr>                                                  
                    <td>{{ contact.get_type_display }}</td>
                    <td>{{ contact.first_name }}</td>
                    <td>{{ contact.company }}</td>                      
               </tr>
               {% endfor %}
      </div>
</div>
1 Answers

You need to fix your context variable:

context = {
            'contacts': contacts,
            'count_supplier': count_supplier,                   
            'suppliers': suppliers,
        }
Related