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

后端 未结 6 851
别那么骄傲
别那么骄傲 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 14:59

    Hey all I used the new path() function and here is my working example that I'm sure will help:

    views.py:

    from django.views.generic.detail import DetailView
    
    class ContentAmpView(DetailView):
    
        model = Content
        template_name = 'content_amp.html'  # Defaults to content_detail.html
    

    urls.py:

    from django.urls import path
    
    from .views import ContentAmpView
    
    # My pk is a string so using a slug converter here intead of int
    urlpatterns = [
        path('/amp', ContentAmpView.as_view(), name='content-amp'),
    ]
    

    templates/content_amp.html

    
    
    
        
        
        Hello, AMPs
        
        
        
        
        
    
    
    

    Welcome to AMP - {{ object.pk }}

    {{ object.titles.main }}

    Reporter: {{ object.reporter }}

    Date: {{ object.created_at|date }}

    Also note that in my settings.py, under TEMPLATES, I have 'APP_DIRS': True. More on path here.

提交回复
热议问题