我第一次使用DRF,想知道网站上以下代码片段中的“format”是什么意思:
class CommentList(APIView):
def get(self, request, format=None):
# do stuff...
def post(self, request, format=None):
# do stuff...
我阅读了文档,但仍不确定它如何工作。有人能用一个例子来解释一下吗?谢谢
我第一次使用DRF,想知道网站上以下代码片段中的“format”是什么意思:
class CommentList(APIView):
def get(self, request, format=None):
# do stuff...
def post(self, request, format=None):
# do stuff...
我阅读了文档,但仍不确定它如何工作。有人能用一个例子来解释一下吗?谢谢
APIView
的类,其代码如下:class HelloAPIView(APIView):
def get(self, request, format):
# do stuff...
def post(self, request, format):
# do stuff...
HelloAPIView的端点URL为:
http://example.com/api/users
http://example.com/api/users.json
在DRF中添加格式以处理多种内容类型
使用格式后缀可以为我们提供明确引用给定格式的URL,并且意味着我们的API将能够处理诸如以下URL:
http://localhost/api/items/4.json
在API的URLconf中为每个条目添加格式后缀模式容易出错且不符合DRY原则,因此REST框架提供了一种快捷方式来将这些模式添加到您的URLConf中。
有关更多详细信息,请参见this
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html'])
这个是怎么运作的呢? - AKJ