Объекты модели Django не отображаются в моем шаблоне

Я новичок в Django. У меня проблема с отображением объектов модели в моем шаблоне

вот мой 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

вот мой 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)

вот код моего шаблона:

{% 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 %}

Вы смешиваете два разных понятия: наборы поколений и экземпляры моделей.

Employee.objects.all(), то есть в настоящее время employee в вашем шаблоне, является кверисетом.

В зависимости от того, чего именно вы хотите добиться, вам нужно итерировать этот набор запросов для доступа к атрибутам каждого экземпляра:

# 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)

Затем для каждого employee из кверисета 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 %}
Вернуться на верх