down vofavori 
<https://stackoverflow.com/questions/51772361/how-to-include-delete-function-in-updateview-instead-a-deleteview#>

I have a UpdateView for editing a post, but instead of making a DeleteView 
for delete a post, i try to make UpdateView include a function to delete 
the post. So ,i want to edit and delete a post in UpdateView. Is that 
possible ? i think i missing something in my code so the code keep getting 
error.

form_valid() missing 1 required positional argument: '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 urlfrom . import viewsfrom 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 %}

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/30679efe-5f30-4417-8492-1d85427cc538%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to