class PostUpdateView(LoginRequiredMixin ,UpdateView): model = Post template_name = 'blog/post_form.html' form_class = PostForm
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['title'] = 'Update' return context
def form_valid(self, form): form.instance.author = self.request.user form.save() return super().form_valid(form)
@login_requireddef post_delete(request, slug): post = get_object_or_404(Post, slug=slug) if (request.user == post.author): post.delete() return redirect('blog:post_list') else: return redirect('blog:post_detail', slug=slug)
--
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 post to this group, send email to django...@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/9b38d4e0-a30a-43ed-9af6-6c9ac545024f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
I’m kind of new to this as well but figured I would take a stab at this. It seems to me that if you wanted to prevent users from deleting posts that weren’t theirs, the appropriate course of action would be to simply remove their ability to access the delete method in the first place. I would recommend placing logic in the front end that only shows the delete option to logged in users who are the original authors of the post.
--
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 post to this group, send email to django...@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/25b558ad-9811-4705-84b4-93dce71d30fb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.