delete button

37 views
Skip to first unread message

Delvin Alexander

unread,
Apr 13, 2022, 12:32:51 AM4/13/22
to Django users
Hello everyone,

i am having trouble. i created a delete button but my page does not confirm the deletion of the post, nor does it redirect to another page. may someone help guide me on what it is i have done wrong and explain for me please?

- I feel as though the problem could be located in my views.py but not sure


Here is my views.py:

from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post


def home(request):
    context = {
        'posts': Post.objects.all()
    }
    return render(request, 'blog/home.html', context)


class PostListView(ListView):
    model = Post
    template_name = 'blog/home.html'
    context_object_name = 'posts'
    ordering = ['-date_posted']


class PostDetailView(DetailView):
    model = Post
   
   
class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    fields = ['title', 'content']
   
    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)
   
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Post
    fields = ['title', 'content']
   
    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)
   
    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        return False
   
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Post
    success_url = 'blog'
   
    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        return False

def about(request):
    return render(request, 'blog/about.html', {'title': 'About'})



Here is my urls.py:

from django.urls import path
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeleteView
from . import views

urlpatterns = [
    path('', PostListView.as_view(), name='blog-home'),
    path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
    path('post/new/', PostCreateView.as_view(), name='post-create'),
    path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
    path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
    path('about/', views.about, name='blog-about'),
]


And here is my post_confirm_delete.html:

{% extends "blog/base.html" %}
{% block content %}
    <div class="content-section">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Delete Post</legend>
                <h2>Are you sure you want to delete this post? "{{ object.title }}"</h2>
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-danger" type="submit">Yes, Delete</button>
                <a class="btn btn-outline-secondary" href="{% url 'post-detail' object.id %}">Cancel</a>  
            </div>
        </form>
    </div>  
{% endblock content %}


      

Antonis Christofides

unread,
Apr 13, 2022, 2:20:20 AM4/13/22
to django...@googlegroups.com

What is `test_func()`?

In addition, if you want people to help you, it would be a good idea to help them by giving them the smallest possible amount of code that reproduces the problem. (In fact, whenever I do that, in about 50% of cases I solve the problem myself during the reduction process.)

Regards,

Antonis

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3a23eabf-f8bd-4fa3-a27b-199a3aabf33bn%40googlegroups.com.

Delvin Alexander

unread,
Apr 13, 2022, 10:58:25 PM4/13/22
to django...@googlegroups.com
test func is a function that my 'User' passes tests mixins runs in django.

Also thank you for informing me to include less. I put 3 files there because i thought someone would have needed it. 

Antonis Christofides

unread,
Apr 14, 2022, 9:54:11 AM4/14/22
to django...@googlegroups.com

Also thank you for informing me to include less. I put 3 files there because i thought someone would have needed it. 

I wasn't clear enough. You should include the entire code that reproduces the problem. Remove stuff from your code until you can't remove any more, while the problem is still there.

Reply all
Reply to author
Forward
0 new messages