Home > Blockchain >  Generic detail view UpdatePostView must be called with either an object pk or a slug in the URLconf
Generic detail view UpdatePostView must be called with either an object pk or a slug in the URLconf

Time:07-10

Trying to implement a update post view to my django blog.

It keeps throwing me the error: 'Generic detail view UpdatePostView must be called with either an object pk or a slug in the URLconf.'

I know it is telling me to call it with a PK or slug, but I'm not sure how to do this.

views.py:

class UpdatePostView(UpdateView):
model = Post
template_name = 'update_post.html'
fields = ('title', 'excerpt', 'slug', 'featured_image',
          'content', 'author', 'status')

urls.py:

from .views import AddPost, PostList, PostDetail, PostLike, UpdatePostView
from django.urls import path

urlpatterns = [
    path('', PostList.as_view(), name='home'),
    path('like/<slug:slug>/', PostLike.as_view(), name='post_like'),
    path('add_post/', AddPost.as_view(), name='create_post'),
    path('edit_post/', UpdatePostView.as_view(), name='update_post'),
    path('<slug:slug>/', PostDetail.as_view(), name='post_detail'),
]

update_post.html:

{% extends "base.html" %}

{% block content %}

<header>
    <div  id="image">
        <div >
            <img  id="main-background"
                src="https://res.cloudinary.com/dhyy9pzrd/image/upload/v1653761818/header-image_q71tuy.jpg"
                alt="blue/pink background image">
            <div >
                <div >
                    <div >
                        <h1 >Update your post</h1>
                        <h6 >blablablabla</h6>
                    </div>
                </div>
            </div>
        </div>
    </div>
</header>

<form  action="." method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button  id="button">Post</button>
</form>

{%endblock%}

CodePudding user response:

answer is in @Waldemar Podsiadio comment

urls.py:

from .views import AddPost, PostList, PostDetail, PostLike, UpdatePostView
from django.urls import path

urlpatterns = [
    path('', PostList.as_view(), name='home'),
    path('like/<slug:slug>/', PostLike.as_view(), name='post_like'),
    path('add_post/', AddPost.as_view(), name='create_post'),
    # for edit you should define slug or pk through /<int:pk>/ or /<pk>/
    path('edit_post/<slug:slug>/', UpdatePostView.as_view(), name='update_post'),
    path('<slug:slug>/', PostDetail.as_view(), name='post_detail'),
]
  • Related