restful 和 restframeword 和 restframeword 序列化
Posted xuerh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了restful 和 restframeword 和 restframeword 序列化相关的知识,希望对你有一定的参考价值。
一 。 restful 请求流程
restful 没有语言限制,
一切皆资源:
通过 请求方式知道要做什么操作 比如(HTTP GET、POST、PUT( 全局更新)/PATCH( 局部更新)、DELETE,还可能包括 HEADER 和 OPTIONS。)
restful (是一种协议,) 一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
def as_view(cls, **initkwargs): """ Main entry point for a request-response process. """ for key in initkwargs: if key in cls.http_method_names: raise TypeError("You tried to pass in the %s method name as a " "keyword argument to %s(). Don‘t do that." % (key, cls.__name__)) if not hasattr(cls, key): raise TypeError("%s() received an invalid keyword %r. as_view " "only accepts arguments that are already " "attributes of the class." % (cls.__name__, key)) def view(request, *args, **kwargs): self = cls(**initkwargs) if hasattr(self, ‘get‘) and not hasattr(self, ‘head‘): self.head = self.get self.request = request self.args = args self.kwargs = kwargs return self.dispatch(request, *args, **kwargs) view.view_class = cls view.view_initkwargs = initkwargs # take name and docstring from class update_wrapper(view, cls, updated=()) # and possible attributes set by decorators # like csrf_exempt from dispatch update_wrapper(view, cls.dispatch, assigned=()) return view
1. 发送一个GET请求到后端, 走url(r‘^books/‘, views.BookView.as_view()),
2. as_view() 自己没有找父类(View)去要
3.as_view方法, return 一个函数名(view) view函数在as_view方法里面 # ( 项目加载,加载到as_View()方法, 客户端发送请求才调用view函数)
4. 执行view函数 返回一个 return self.dispatch(request, *args, **kwargs)
5. dispatch函数
看你什么请求执行对应的函数
def dispatch(self, request, *args, **kwargs): # Try to dispatch to the right method; if a method doesn‘t exist, # defer to the error handler. Also defer to the error handler if the # request method isn‘t on the approved list. if request.method.lower() in self.http_method_names: handler = getattr(self, request.method.lower(), self.http_method_not_allowed) else: handler = self.http_method_not_allowed return handler(request, *args, **kwargs)
二. frestful 中 APIView的请求流程;
1. APIView继承的是 View
2。 1. 发送一个GET请求到后端, 走url(r‘^books/‘, views.BookDetailView.as_view()),
3. as_view 自己没有 就去找它的父类(APIView)去要,
4. APIView 中的as_view 方法执行 view = super(APIView, cls).as_view(**initkwargs) 去找它的父类(view)中的as_view
5. view中 as_view执行view函数 返回一个 return self.dispatch(request, *args, **kwargs)
6.dispatch方法APIView自己有所以就用自己的
7. dispatch中的
request = self.initialize_request(request, *args, **kwargs)
def initialize_request(self, request, *args, **kwargs): """ Returns the initial request object. """ parser_context = self.get_parser_context(request) return Request( request, parsers=self.get_parsers(), authenticators=self.get_authenticators(), negotiator=self.get_content_negotiator(), parser_context=parser_context ) class Request(object): """ Wrapper allowing to enhance a standard `HttpRequest` instance. Kwargs: - request(HttpRequest). The original request instance. - parsers_classes(list/tuple). The parsers to use for parsing the request content. - authentication_classes(list/tuple). The authentications used to try authenticating the request‘s user. """ def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=None): assert isinstance(request, HttpRequest), ( ‘The `request` argument must be an instance of ‘ ‘`django.http.HttpRequest`, not `{}.{}`.‘ .format(request.__class__.__module__, request.__class__.__name__) ) self._request = request self.parsers = parsers or () self.authenticators = authenticators or () self.negotiator = negotiator or self._default_negotiator() self.parser_context = parser_context self._data = Empty self._files = Empty self._full_data = Empty self._content_type = Empty self._stream = Empty
self.request = request
8. 在BookDetailView中调用:
request._request.GET 就是 request.GET
jango 中为了方便request._request.GET 可以简写为 request.GET
四 restframeword 的基本语句和介绍
注意点 项目中的settings 中要设置
INSTALLED_APPS = [
‘rest_framework‘,
]
以上是关于restful 和 restframeword 和 restframeword 序列化的主要内容,如果未能解决你的问题,请参考以下文章
AJAX、RESTful/Rest、JSON 和 JSONP 有啥区别?