Deeps HIT
Deeps HIT

Reputation: 73

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

Cant get access to data in my model, getting next error: AttributeError: Generic detail view Myview must be called with either an object pk or a slug.

My model:

class product(models.Model):
    title = models.CharField(max_length = 1000)
    description = models.TextField(max_length = 5000)
    price = models.IntegerField()

my views:

class Myview(DetailView):
    queryset = product.objects.all()
    template_name = 'templates/products.html'

my urls:

urlpatterns = [
    url(r'^products/', Myview.as_view(), name='products'),
   ]

If there is any another legit way to get data from my model i can change my views and urls correct way.

Upvotes: 1

Views: 1164

Answers (2)

Diego Cortes
Diego Cortes

Reputation: 1

Inside the URL you must deliver the pk as a parameter to obtain the element

url(r'^products/(?P<pk>\d+)/$', Myview.as_view(), name='products'),

Upvotes: 0

Selcuk
Selcuk

Reputation: 59184

Generic DetailView is for fetching info about a single instance of your model.

Since you are fetching all products (in line product.objects.all()) it looks like you want to display a list of products. In this case you must use ListView.

class MyView(ListView):
    model = Product
    template_name = 'templates/products.html'

Upvotes: 1

Related Questions