'Django Model objects not displaying in my template

I am new to Django. I have problem in displaying model objects into my template

here is my models.py:

from django.db import models
from django.contrib.auth.models import User

class Employee(models.Model):
    username = models.TextField(max_length=100)
    email = models.CharField(max_length=100)
    password = models.TextField(max_length=100)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
         return self.username

here is my views.py:

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .models import Employee

@login_required
def home(request):
    context ={
         'employee': Employee.objects.all()
    }
    return render(request, 'newusers/home.html', context)

here is my template code:

{% extends "users/base.html" %} {% load crispy_forms_tags %} {% block content %}
<article class="media content-section">
  <div class="media-body">
    <div class="card">
      <a class="mr-2" href="#">{{ employee.username }}</a>
      <small class="text-muted">{{ employee.email }}</small>
    </div>
  </div>
</article>
{% endblock content %}


Solution 1:[1]

You get two distinct notions mixed up: querysets and model instances.

Employee.objects.all(), that is to say currently employee in your template, is a queryset.

Depending on what you exactly want to achieve, you need to iterate this queryset to access each instance attributes:

# views.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .models import Employee

@login_required
def home(request):
    context ={'employees': Employee.objects.all()}  # note the s at the end
    # here you may want to print `list(context["employees"])` to understand the queryset structure
    return render(request, 'newusers/home.html', context)

And then for each employee of the queryset employees:

{% block content %}
{% for employee in employees %}
<article class="media content-section">
  <div class="media-body">
    <div class="card">
      <a class="mr-2" href="#">{{ employee.username }}</a>
      <small class="text-muted">{{ employee.email }}</small>
    </div>
  </div>
</article>
{% endfor %}
{% endblock %}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sc?riolus