7
7
7a-6662020-05-26 08:19:20
Django
7a-666, 2020-05-26 08:19:20

How to add post method to DetailView?

There is a view with the DetailView class, if you send a form with the post method to it, then the HTTP error ERROR 405 , as far as I understand, in the DetailView class there is no post method in the documentation, I read about http_method_names and defined it , the same error I don’t want to use the get method for filters, since the url string becomes Is it possible to somehow define the post method in this view with the DetailView class ?http_method_names = ['get', 'post']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
i_am_duderje, 2020-05-26
@7a-666

in the first answer they correctly said that you need to implement the post method on your view inherited from DetailView, somewhere like this:

class BlogDetailView(DetailView):
 template_name= 'blog-info.html'
 model = Blog
 def post(self, request, *args, **kwargs):
   # тут ваши действия

All http methods must return an HttpResponse object
. Of course, there are times when it's more convenient to handle POST requests in a DetailView.
For example, subscribe to a blog while viewing it. Display the blog through the DetailView, and when you click on the "subscribe" button, send the form with a POST request to the same blog view page. And in the DetailView of your blog, implement the post() method, which will add a subscription to this blog to the current user.
and by the way, http_method_names should be used when you want to set up specific methods. Standard HTTP methods are already in http_method_names by default.
But the filtering becomes certainly through GET.
What is wrong - correct :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question