views.py 844 B

12345678910111213141516171819202122232425262728293031
  1. from django.core.files import File
  2. from django.core.serializers.json import DjangoJSONEncoder
  3. from django.http import JsonResponse
  4. from django.views import generic
  5. from tests.testapp import forms
  6. class FileEncoder(DjangoJSONEncoder):
  7. def default(self, o):
  8. if isinstance(o, File):
  9. return o.name
  10. super().default(o)
  11. class ExampleFormView(generic.FormView):
  12. form_class = forms.UploadForm
  13. template_name = "form.html"
  14. def form_valid(self, form):
  15. return JsonResponse(
  16. {
  17. "POST": self.request.POST,
  18. "FILES": {
  19. "file": self.request.FILES.getlist("file"),
  20. "other_file": self.request.FILES.getlist("other_file"),
  21. },
  22. },
  23. status=201,
  24. encoder=FileEncoder,
  25. )