V
V
Vova1357982021-06-08 11:40:45
Django
Vova135798, 2021-06-08 11:40:45

Why does it throw an error Cannot resolve keyword 'slug' into field.?

When going to the page of the Book model, it throws an error FieldError at /book/pikovaya_dama
Cannot resolve keyword 'slug' into field. Choices are: author, author_id, creation_date, genre, genre_id, id, photo, slag, summary, title

urls.py 

urlpatterns = [
    path('', homepage),
    path('book/<slug:slug>', BookDetailView.as_view(), name='book'),
]

models.py

from django.db import models
from django.urls import reverse


class Author(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    date_of_birth = models.DateField(null=True, blank=True)
    date_of_death = models.DateField('died', null=True, blank=True)
    photo = models.ImageField(upload_to='authors/%m/%d', null=True, blank=True)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return '{0} {1}'.format(self.first_name, self.last_name)


class Genre(models.Model):
    title = models.CharField(max_length=155)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return self.title


class Book(models.Model):
    title = models.CharField(max_length=155, verbose_name='Название книги')
    summary = models.TextField(blank=True, verbose_name='Краткое описание')
    photo = models.ImageField(upload_to='books/%m/%d', null=True, blank=True)
    genre = models.ForeignKey(Genre, on_delete=models.CASCADE, null=True)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, null=True)
    creation_date = models.DateField(blank=True, verbose_name='Дата создания')
    slag = models.SlugField(unique=True)

    def get_absolute_url(self):
        return reverse('book', args=[self.slag])

    def __str__(self):
        return self.title

view.spy

from django.shortcuts import render, HttpResponse
from .models import Book, Author, Genre
from django.views.generic import DetailView


def homepage(request):
    books = Book.objects.all()
    authors = Author.objects.all()
    now = datetime.datetime.now()
    context = {
        'books': books,
        'authors': authors,
    }
    return render(request, 'main/homepage.html', context=context)


class BookDetailView(DetailView):
    model = Book
    context_object_name = 'item'
    template_name = 'main/bookview.html'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexandrBirukov, 2021-06-08
@Vova135798

so it seems there is no slug field in the Book model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question