💻
Bookstores
  • Tech Stacks
  • Django
    • Django REST framework
    • Serializer validation
    • Form validation in view class
  • Use Open API data model
  • Server Side react javascript
  • React context and redux difference
  • Migrate state management from Redux to Jotai
  • Migrate from REST to GraphQL
  • Migrate from REST to gRPC
  • How to handle cross origin?
Powered by GitBook
On this page

Was this helpful?

  1. Django

Django REST framework

PreviousDjangoNextSerializer validation

Last updated 4 years ago

Was this helpful?

  • The source repository is

  • Installation pip install djangorestframework

  • In the regular django, you need to implement view in order to output data. with REST framework, only need to implement serializer book.serializers.py in order to save data. And, register url in

    book.urls.py When server startup, then you can view api in

  • Snake-case to camel-case Python is a language like all against c syntax, no curly brace block match and use snake-case for the variables. In general, we also use camel-case in javascript, json. The following implementation totally take the extra translation.

    Add

      utils.api.parsers.py
      utils.api.renders.py

    This parser was copied from .

    So in the settings.py, also update:

      REST_FRAMEWORK = {
          'DEFAULT_RENDERER_CLASSES': (
              'utils.api.renderers.CamelCaseJSONRenderer',
              'rest_framework.renderers.BrowsableAPIRenderer',
          ),
    
          'DEFAULT_PARSER_CLASSES': (
              'utils.api.parsers.CamelCaseJSONRenderer',
              'rest_framework.parsers.FormParser',
              'rest_framework.parsers.MultiPartParser'
          ),
      }
  • To bypass cross Origin, you need to add the following middleware implementation into order to have browser accept the cross site permission. Middleware implement at bookstore_api.corsapp.middleware.py

      class CorsMiddleware(object):
    
          def __init__(self, get_response):
              self.get_response = get_response
    
          def __call__(self, request):
              response = self.get_response(request)
              response["Access-Control-Allow-Origin"] = "*"
              response["Access-Control-Allow-Headers"] = "*"
              response["Access-Control-Allow-Methods"] = "*"
    
              return response
https://github.com/ccapeng/bookstore_api
http://127.0.0.1:8000/api/category/
http://127.0.0.1:8000/api/publisher/
http://127.0.0.1:8000/api/author/
http://127.0.0.1:8000/api/book/
https://gist.github.com/vbabiy/5842073