根据请求头 content-type 选择对应的解析器对请求体内容进行处理。
有application/json,x-www-form-urlencoded,form-data等格式
| REST_FRAMEWORK = { |
| 'DEFAULT_PARSER_CLASSES':[ |
| 'rest_framework.parsers.JSONParser' |
| 'rest_framework.parsers.FormParser' |
| 'rest_framework.parsers.MultiPartParser' |
| ] |
| |
| } |
| urlpatterns = [ |
| url(r'test/', TestView.as_view()), |
| ] |
| from rest_framework.views import APIView |
| from rest_framework.response import Response |
| |
| class TestView(APIView): |
| def post(self, request, *args, **kwargs): |
| print(request.content_type) |
| |
| |
| print(request.data) |
| |
| print(request.POST) |
| print(request.FILES) |
| return Response('POST请求,响应内容') |
| |
| def put(self, request, *args, **kwargs): |
| return Response('PUT请求,响应内容') |
| from django.conf.urls import url, include |
| from web.views.s5_parser import TestView |
| |
| urlpatterns = [ |
| url(r'test/', TestView.as_view(), name='test'), |
| ] |
| |
| |
| from rest_framework.views import APIView |
| from rest_framework.response import Response |
| from rest_framework.request import Request |
| from rest_framework.parsers import JSONParser |
| |
| class TestView(APIView): |
| parser_classes = [JSONParser, ] |
| |
| def post(self, request, *args, **kwargs): |
| print(request.content_type) |
| |
| |
| print(request.data) |
| |
| |
| print(request.POST) |
| print(request.FILES) |
| |
| return Response('POST请求,响应内容') |
| |
| def put(self, request, *args, **kwargs): |
| return Response('PUT请求,响应内容') |
| from django.conf.urls import url, include |
| from web.views import TestView |
| |
| urlpatterns = [ |
| url(r'test/', TestView.as_view(), name='test'), |
| ] |
| |
| |
| from rest_framework.views import APIView |
| from rest_framework.response import Response |
| from rest_framework.request import Request |
| from rest_framework.parsers import FormParser |
| |
| class TestView(APIView): |
| parser_classes = [FormParser, ] |
| |
| def post(self, request, *args, **kwargs): |
| print(request.content_type) |
| |
| |
| print(request.data) |
| |
| |
| print(request.POST) |
| print(request.FILES) |
| |
| return Response('POST请求,响应内容') |
| |
| def put(self, request, *args, **kwargs): |
| return Response('PUT请求,响应内容') |
| from django.conf.urls import url, include |
| from web.views import TestView |
| |
| urlpatterns = [ |
| url(r'test/', TestView.as_view(), name='test'), |
| ] |
| |
| |
| from rest_framework.views import APIView |
| from rest_framework.response import Response |
| from rest_framework.request import Request |
| from rest_framework.parsers import MultiPartParser |
| |
| class TestView(APIView): |
| parser_classes = [MultiPartParser, ] |
| |
| def post(self, request, *args, **kwargs): |
| print(request.content_type) |
| |
| |
| print(request.data) |
| |
| print(request.POST) |
| print(request.FILES) |
| return Response('POST请求,响应内容') |
| |
| def put(self, request, *args, **kwargs): |
| return Response('PUT请求,响应内容') |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| |
| <title>Title</title> |
| </head> |
| <body> |
| <form action="http://127.0.0.1:8000/test/" method="post" enctype="multipart/form-data"> |
| <input type="text" name="user" /> |
| <input type="file" name="img"> |
| |
| <input type="submit" value="提交"> |
| |
| </form> |
| </body> |
| </html> |
| from django.conf.urls import url, include |
| from web.views import TestView |
| |
| urlpatterns = [ |
| url(r'test/(?P<filename>[^/]+)', TestView.as_view(), name='test'), |
| ] |
| |
| |
| from rest_framework.views import APIView |
| from rest_framework.response import Response |
| from rest_framework.request import Request |
| from rest_framework.parsers import FileUploadParser |
| |
| class TestView(APIView): |
| parser_classes = [FileUploadParser, ] |
| |
| def post(self, request, filename, *args, **kwargs): |
| print(filename) |
| print(request.content_type) |
| |
| |
| print(request.data) |
| |
| print(request.POST) |
| print(request.FILES) |
| return Response('POST请求,响应内容') |
| |
| def put(self, request, *args, **kwargs): |
| return Response('PUT请求,响应内容') |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| |
| <title>Title</title> |
| </head> |
| <body> |
| <form action="http://127.0.0.1:8000/test/f1.numbers" method="post" enctype="multipart/form-data"> |
| <input type="text" name="user" /> |
| <input type="file" name="img"> |
| |
| <input type="submit" value="提交"> |
| |
| </form> |
| </body> |
| </html> |
当同时使用多个parser时,rest framework会根据请求头content-type自动进行比对,并使用对应parser
| from django.conf.urls import url, include |
| from web.views import TestView |
| |
| urlpatterns = [ |
| url(r'test/', TestView.as_view(), name='test'), |
| ] |
| |
| |
| from rest_framework.views import APIView |
| from rest_framework.response import Response |
| from rest_framework.request import Request |
| from rest_framework.parsers import JSONParser, FormParser, MultiPartParser |
| |
| class TestView(APIView): |
| parser_classes = [JSONParser, FormParser, MultiPartParser, ] |
| |
| def post(self, request, *args, **kwargs): |
| print(request.content_type) |
| |
| |
| print(request.data) |
| |
| print(request.POST) |
| print(request.FILES) |
| return Response('POST请求,响应内容') |
| |
| def put(self, request, *args, **kwargs): |
| return Response('PUT请求,响应内容') |
| |
| @property |
| def data(self): |
| if not _hasattr(self, '_full_data'): |
| self._load_data_and_files() |
| return self._full_data |
| |
| |
| |
| def _parse(self): |
| |
| media_type = self.content_type |
| |
| |
| |
| parser = self.negotiator.select_parser(self, self.parsers) |
| |
| |
| def select_parser(self, request, parsers): |
| |
| |
| for parser in parsers: |
| if media_type_matches(parser.media_type, request.content_type): |
| return parser |
| return None |
| |
| |
| |
| Request( |
| request, |
| parsers=self.get_parsers(), |
| authenticators=self.get_authenticators(), |
| negotiator=self.get_content_negotiator(), |
| parser_context=parser_context |
| ) |
| |
| def get_parsers(self): |
| return [parser() for parser in self.parser_classes] |
| |
| |
| parser_classes = api_settings.DEFAULT_PARSER_CLASSES |
| |
| def __getattr__(self, attr): |
| if attr not in self.defaults: |
| raise AttributeError("Invalid API setting: '%s'" % attr) |
| |
| try: |
| |
| val = self.user_settings[attr] |
| except KeyError: |
| |
| val = self.defaults[attr] |
| |
| |
| if attr in self.import_strings: |
| val = perform_import(val, attr) |
| |
| |
| self._cached_attrs.add(attr) |
| setattr(self, attr, val) |
| return val |
| |
| @property |
| def user_settings(self): |
| if not hasattr(self, '_user_settings'): |
| self._user_settings = getattr(settings, 'REST_FRAMEWORK', {}) |
| return self._user_settings |
| const webpack = require("webpack"); |
| |
| module.exports = { |
| configureWebpack: { |
| plugins: [ |
| new webpack.ProvidePlugin({ |
| $: "jquery", |
| jQuery: "jquery", |
| "window.jQuery": "jquery", |
| Popper: ["popper.js", "default"] |
| }) |
| ] |
| } |
| }; |
| >: cnpm install bootstrap@3 |
| import "bootstrap" |
| import "bootstrap/dist/css/bootstrap.css" |