Django: Generic detail view must be called with either an object pk or a slug

后端 未结 6 843
别那么骄傲
别那么骄傲 2021-02-01 14:26

Getting this error when submitting the form associated with this view. Not sure what exactly is the problem, considering I have a form with a very similar structure and it works

6条回答
  •  再見小時候
    2021-02-01 15:08

    I am using Django version 2.2.12,

    Here is just another example this works fine for me :

    models.py

    from django.db import models
    
    class Book(models.Model):
        title = models.CharField(max_length=256)
        author = models.CharField(max_length=256)
        pages = models.IntegerField()
        price = models.FloatField()
    

    views.py

    from django.views.generic import ListView,DetailView
    from testapp.models import Book
    
    class BookListView(ListView):
        model=Book
    
    class BookDetailView(DetailView):
        model=Book
    

    urls.py

    from django.contrib import admin
    from django.urls import path
    from testapp import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', views.BookListView.as_view()),
        path('/', views.BookDetailView.as_view()),
    ]
    

提交回复
热议问题