DEV Community

Phansivang
Phansivang

Posted on

Django Delete Post (Delete view)

views.py

from django.views.generic import DetailView, DeleteView
from .models import Post

class deletpost(DeleteView):
    model = Post
    success_url = '/'

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        return False
Enter fullscreen mode Exit fullscreen mode

urls.py

from django.urls import path
from . import views

urlpatterns = [
path('post/<int:pk>/delete',views.deletpost.as_view(),name='delete-post')

]

Enter fullscreen mode Exit fullscreen mode

create html go by name post_confirm_delete.html

{% block content %}
    <form method="POST">
    {% csrf_token %}
        <h1>Are you sure?</h1>
    <button type="submit">Delete</button>
    <a href="{% url 'home' %}">Cancel</a>
    </form>
{% endblock %}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)