From 25c016b3dfca21bd7d43fef22631b6a12e61de8c Mon Sep 17 00:00:00 2001 From: Gavin Vickery Date: Thu, 26 May 2022 16:01:53 -0700 Subject: [PATCH] Created example app, adding example for html arrays. --- apps/examples/__init__.py | 0 apps/examples/apps.py | 6 ++++++ apps/examples/migrations/__init__.py | 0 apps/examples/serializers.py | 7 +++++++ apps/examples/urls.py | 8 ++++++++ apps/examples/views.py | 26 ++++++++++++++++++++++++++ project/settings.py | 4 ++++ project/urls.py | 3 +++ 8 files changed, 54 insertions(+) create mode 100644 apps/examples/__init__.py create mode 100644 apps/examples/apps.py create mode 100644 apps/examples/migrations/__init__.py create mode 100644 apps/examples/serializers.py create mode 100644 apps/examples/urls.py create mode 100644 apps/examples/views.py diff --git a/apps/examples/__init__.py b/apps/examples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/examples/apps.py b/apps/examples/apps.py new file mode 100644 index 0000000..2f06e10 --- /dev/null +++ b/apps/examples/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ExamplesConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'apps.examples' diff --git a/apps/examples/migrations/__init__.py b/apps/examples/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/examples/serializers.py b/apps/examples/serializers.py new file mode 100644 index 0000000..83f7c30 --- /dev/null +++ b/apps/examples/serializers.py @@ -0,0 +1,7 @@ +from rest_framework import serializers + + +class HTMLArraySerializer(serializers.Serializer): + name = serializers.CharField() + hours_open = serializers.IntegerField() + foods = serializers.ListField() diff --git a/apps/examples/urls.py b/apps/examples/urls.py new file mode 100644 index 0000000..061eec6 --- /dev/null +++ b/apps/examples/urls.py @@ -0,0 +1,8 @@ +from django.urls import path + +from . import views + + +urlpatterns = [ + path('examples/html-array', views.HTMLArray.as_view(), name='examples-html-array'), +] diff --git a/apps/examples/views.py b/apps/examples/views.py new file mode 100644 index 0000000..2a7665f --- /dev/null +++ b/apps/examples/views.py @@ -0,0 +1,26 @@ +from rest_framework import views, permissions +from rest_framework.response import Response +from rest_framework.parsers import FormParser, MultiPartParser + +from .serializers import HTMLArraySerializer + + +class HTMLArray(views.APIView): + """ + An example of handling an array of fields posted from an HTML form: + + + + + """ + permission_classes = [permissions.AllowAny] + parser_classes = [FormParser, MultiPartParser] + + def post(self, request, format=None): + data = request.data.copy() + data['foods'] = request.data.getlist('foods[]') # Must be converted to list, not QueryDict + serializer = HTMLArraySerializer(data) + + # Here we're just returning the value for visibility, but you would normally use the + # serializer to insert into db. + return Response(serializer.data) diff --git a/project/settings.py b/project/settings.py index 20bca39..8862cdb 100644 --- a/project/settings.py +++ b/project/settings.py @@ -54,6 +54,10 @@ 'apps.mail', 'apps.user', 'apps.workers', + + # Examples + # !!! REMOVE ME IN PRODUCTION !!! + 'apps.examples', ] MIDDLEWARE = [ diff --git a/project/urls.py b/project/urls.py index 360a1cb..088c16d 100644 --- a/project/urls.py +++ b/project/urls.py @@ -17,6 +17,9 @@ path('', include('apps.file.urls')), path('', include('apps.user.urls')), + # Examples + path('', include('apps.examples.urls')), + # DRF API path('api/', api_root, name='index'), path('api-auth/', include('rest_framework.urls')),