Я пытался показать данные модели в html-шаблоне в django.
Моя модель:
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
date_of_birth = models.DateField(blank=True, null=True)
date_of_death = models.DateField(blank=True, null=True)
def get_absolute_url(self):
return reverse('author_detail', args=[str(self.id)])
class Meta():
ordering = ['first_name', 'last_name']
def __str__(self):
return f'{self.first_name} {self.last_name}'
Мой вид:
def author_detail_view(request, pk):
author = get_object_or_404(Author, pk=pk)
return render(request, 'author_detail.html', context={'author_detail': author})
Мой URL:
path('author/<int:pk>', views.author_detail_view, name='author_detail')
And My Templates View:
{% extends 'base.html' %}
{% block content %}
<h1>Author Detail</h1>
{% for author in author_detail %}
<ul>
<li>Name: {{ author.first_name }} {{ author.last_name }}</li>
<li>Date of Birth: {{ author.date_of_birth }}</li>
</ul>
{% endfor %}
{% endblock %}
Но проблема в том, что очень малая ошибка:
TypeError at / author /2
Объект 'Author' не повторяется
Метод запроса: GET URL запроса: http://127.0.0.1:8000/author/2 Версия Django: 2.1.5 Тип исключения: TypeError Значение исключения:
Объект 'Author' не повторяется
Местоположение исключения: /home/pyking/.local/lib/python3.6/site-packages/django/template/defaulttags.py в рендере, строка 165 Исполняемый файл Python: / usr / bin / python3 Версия Python: 3.6.7