Anshul M
Anshul M

Reputation: 99

Django 2.1.2: Generic detail view SchoolDetailView must be called with either an object pk or a slug in the URLconf

I am new in django and didn't understand what is pk or slug. What is going on?

models.py:

class School(models.Model):
    name = models.CharField(max_length=256)
    principal = models.CharField(max_length=256)
    location = models.CharField(max_length=256)

    def __str__(self):
        return self.name

Template Page:

<a class="navbar-brand" href="{% url 'basic_app:list'%}">Schools</a>

urls.py:

path('',views.SchoolDetailView.as_view(),name='list'),

views.py:

class SchoolDetailView(DetailView):
    context_object_name = 'school_detail'
    model = models.School
    template_name = 'basic_app/school_detail.html'

Upvotes: 0

Views: 3603

Answers (1)

mohammed wazeem
mohammed wazeem

Reputation: 1328

Detail view is used for fetching detail of a particular object. Inorder to do that you have to pass pk in your url.

urlpatterns = [
path('<int:pk>/', SchoolDetailView.as_view(), name='school-detail'),

]

If you want to display the list of object use ListView

Upvotes: 3

Related Questions