POST form response in subclass DetailView

I have a view that uses one of the class-based generic views (derived from django.views.generic.DetailView) with a custom form:

from django.views.generic import DetailView class MyDetailView(DetailView): model=MyModel def get_object(self): object = MyModel.objects.get(uuid=self.kwargs['uuid']) def get_context_data(self, **kwargs): form = MyForm context = super(MyDetailView, self).get_context_data(**kwargs) context['form'] = form return context 

I would like POST to submit the form sent to the same URL as GET. Is it possible to change this view code so that it can also respond to POST? If so, how? Do I need to inherit from another mixin class? And what is the name of the method you need to add to process the form data?

Or is it just a bad idea to respond to POST at the same URL for this script?

(Note: the form does not change the displayed MyModel, but is used to create a model object of a different type, so UpdateView is not very suitable).

+4
source share
2 answers

It's not entirely clear what you want to do with your POST request, but it makes sense to use django.views.generic.UpdateView instead of DetailView - since it also gives you some methods for processing forms.

To simply add POST request processing to the detail view, the post() method should be sufficient!

+9
source

Looking at the Django code, the post () method should look like this:

 def post(self, request, *args, **kwargs): ... 
+4
source

Source: https://habr.com/ru/post/1390421/


All Articles