I have a view that uses one of the class-based generic views (derives 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'd like the POST for the form submission to go to the same URL as the GET. Is it possible to modify this view code so that it can also respond to the POST? If so, how? Do I need to inherit from another mixin class? And what is the name of the method to add to process the form data?
Or is it just a bad idea to respond to the POST at the same URL for this scenario?
(Note: the form is not modifying the MyModel object being displayed. Instead, it's being used to create a model object of a different type, so UpdateView isn't reallya good fit).
It's not quite clear what you want to do with your POST request, but it might make sense to use django.views.generic.UpdateView
instead of DetailView
- as it also gives you some methods to deal with form processing.
To just add handling of a POST request to the detail view a post()
method should be enough!
Looking at the Django code, the post() method should look like:
def post(self, request, *args, **kwargs):
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With