如何在UpdateView中包含删除功能,而不是DeleteView

时间:2018-08-09 16:56:32

标签: python django django-models django-forms django-views

我有一个用于编辑帖子的UpdateView,但是我没有使DeleteView删除帖子,而是尝试使UpdateView包含删除帖子的功能。所以,我想编辑和删除UpdateView中的帖子。那可能吗 ?我认为我在代码中缺少某些内容,因此代码不断出错。

  

form_valid()缺少1个必需的位置参数:“ pk”

views.py

class PostUpdate(LoginRequiredMixin,generic.UpdateView):
    model = PostModel
    fields = ['title','file','description']
    template_name = 'post/post_update.html'
    success_url = reverse_lazy('user_profile:profile')

    def form_valid(self, form, pk):
        if 'confirm_post' in self.request.POST:
            form.instance.user = self.request.user
        elif 'confirm_delete' in self.request.POST:
            post_delete = PostModel.objects.get(pk = id)
            post_delete.delete()
        return super(PostUpdate, self).form_valid(form)

urls.py

from django.conf.urls import url
from . import views
from django.urls import path
app_name = 'post'

urlpatterns = [
    url('user_post/', views.PostView.as_view(), name= 'user_post'),
    url('post_list/', views.PostList.as_view(), name='post_list'),
    path('<int:pk>', views.PostDetail.as_view(), name= 'post_detail'),
    path('update/<int:pk>/', views.PostUpdate.as_view(), name= 'post_update'),
]

post_update.html

{% extends 'base.html'%}
{% load bootstrap3%}
{% block content %}

  <form method="POST" enctype="multipart/form-data">
    {% csrf_token %}

    {% bootstrap_form form%}
    <input type="submit" name="confirm_post" value="Confirm">
    <input type="submit" name="confirm_delete" value="Delete">
  </form>
{% endblock %}

3 个答案:

答案 0 :(得分:1)

这是一篇旧文章,但是自从解决了这个问题后,我就想回答。

只需利用DeletionMixin文件中的django.generic.edit模块。

from django.views.generic.edit import LoginRequiredMixin, UpdateView, DeletionMixin


class PostUpdate(LoginRequiredMixin, DeletionMixin, UpdateView):
    model = PostModel
    fields = ['title','file','description']
    template_name = 'post/post_update.html'
    success_url = reverse_lazy('user_profile:profile')



    def post(self, request, *args, **kwargs):
        if "confirm_delete" in self.request.POST:
            return self.delete(request, *args, **kwargs)

    def form_valid(self, form, pk):
        if 'confirm_post' in self.request.POST:
            form.instance.user = self.request.user
        return super(PostUpdate, self).form_valid(form)

DeletionMixin将导入其delete函数并处理其余函数。确保覆盖post函数,否则所有POST请求都将导致删除。

答案 1 :(得分:0)

def form_valid(self, form, pk):行应为def form_valid(self, form):The docs的form_valid不带有pk参数。您可以使用form.instance.pk来获取pk。

编辑:

class PostUpdate(LoginRequiredMixin,generic.UpdateView):
    model = PostModel
    fields = ['title','file','description']
    template_name = 'post/post_update.html'
    success_url = reverse_lazy('user_profile:profile')

    def form_valid(self, form):

        if 'confirm_post' in self.request.POST:
            form.instance.user = self.request.user
        elif 'confirm_delete' in self.request.POST:
            pk = form.instance.pk
            post_delete = PostModel.objects.get(pk=pk)
            post_delete.delete()
            # Or just one line: form.instance.delete()
        return super(PostUpdate, self).form_valid(form)

答案 2 :(得分:0)

您可以覆盖post方法以使其如下所示工作

class PostUpdate(LoginRequiredMixin,generic.UpdateView):
    model = PostModel
    fields = ['title','file','description']
    template_name = 'post/post_update.html'
    success_url = reverse_lazy('user_profile:profile')

    def post(self, request, pk):
        if 'confirm_delete' in self.request.POST:
            post_delete = PostModel.objects.get(pk=pk)
            post_delete.delete()
        return super(PostUpdate, self).post(request, pk)

    def form_valid(self, form):
        if 'confirm_post' in self.request.POST:
            form.instance.user = self.request.user
        return super(PostUpdate, self).form_valid(form)