1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Refs #30997 -- Added HttpRequest.accepts().

This commit is contained in:
Claude Paroz
2019-11-17 13:24:10 +01:00
committed by Mariusz Felisiak
parent cf493e5c81
commit d66d72f956
5 changed files with 192 additions and 12 deletions

View File

@@ -222,41 +222,43 @@ to edit, and override
aren't logged in from accessing the form. If you omit that, you'll need to
handle unauthorized users in :meth:`~.ModelFormMixin.form_valid()`.
AJAX example
============
.. _content-negotiation-example:
Content negotiation example
===========================
Here is an example showing how you might go about implementing a form that
works for AJAX requests as well as 'normal' form POSTs::
works with an API-based workflow as well as 'normal' form POSTs::
from django.http import JsonResponse
from django.views.generic.edit import CreateView
from myapp.models import Author
class AjaxableResponseMixin:
class JsonableResponseMixin:
"""
Mixin to add AJAX support to a form.
Mixin to add JSON support to a form.
Must be used with an object-based FormView (e.g. CreateView)
"""
def form_invalid(self, form):
response = super().form_invalid(form)
if self.request.is_ajax():
return JsonResponse(form.errors, status=400)
else:
if self.request.accepts('text/html'):
return response
else:
return JsonResponse(form.errors, status=400)
def form_valid(self, form):
# We make sure to call the parent's form_valid() method because
# it might do some processing (in the case of CreateView, it will
# call form.save() for example).
response = super().form_valid(form)
if self.request.is_ajax():
if self.request.accepts('text/html'):
return response
else:
data = {
'pk': self.object.pk,
}
return JsonResponse(data)
else:
return response
class AuthorCreate(AjaxableResponseMixin, CreateView):
class AuthorCreate(JsonableResponseMixin, CreateView):
model = Author
fields = ['name']