rest_framework.views rest_framework.response rest_framework <span style="color: #0000ff;">from rest_framework.renderers <span style="color: #0000ff;">import<span style="color: #000000;"> JSONRenderer
<span style="color: #0000ff;">from .. <span style="color: #0000ff;">import<span style="color: #000000;"> models
<span style="color: #0000ff;">class<span style="color: #000000;"> TestSerializer(serializers.ModelSerializer):
<span style="color: #0000ff;">class<span style="color: #000000;">
Meta:
model =<span style="color: #000000;"> models.UserInfo
fields = <span style="color: #800000;">"<span style="color: #800000;">
all<span style="color: #800000;">"
<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
renderer_classes =<span style="color: #000000;"> [JSONRenderer,]
</span><span style="color: #0000ff;">def</span> get(self,**<span style="color: #000000;">kwargs):
user_list </span>=<span style="color: #000000;"> models.UserInfo.objects.all()
ser </span>= TestSerializer(instance=user_list,many=<span style="color: #000000;">True)
</span><span style="color: #0000ff;">return</span> Response(ser.data)</pre>
2. 表格
访问URL:
@H_404_510@http://127.0.0.1:8000/test/?format=admin
@H_404_510@http://127.0.0.1:8000/test.admin
@H_404_510@http://127.0.0.1:8000/test/
rest_framework.views rest_framework.response rest_framework <span style="color: #0000ff;">from rest_framework.renderers <span style="color: #0000ff;">import<span style="color: #000000;"> AdminRenderer
<span style="color: #0000ff;">from .. <span style="color: #0000ff;">import<span style="color: #000000;"> models
<span style="color: #0000ff;">class<span style="color: #000000;"> TestSerializer(serializers.ModelSerializer):
<span style="color: #0000ff;">class<span style="color: #000000;">
Meta:
model =<span style="color: #000000;"> models.UserInfo
fields = <span style="color: #800000;">"<span style="color: #800000;">
all<span style="color: #800000;">"
<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
renderer_classes =<span style="color: #000000;"> [AdminRenderer,many=<span style="color: #000000;">True)
<span style="color: #0000ff;">return Response(ser.data)
3. Form表单
访问URL:
@H_404_510@http://127.0.0.1:8000/test/?format=form
@H_404_510@http://127.0.0.1:8000/test.form
@H_404_510@http://127.0.0.1:8000/test/
rest_framework.views rest_framework.response rest_framework <span style="color: #0000ff;">from rest_framework.renderers <span style="color: #0000ff;">import<span style="color: #000000;"> JSONRenderer
<span style="color: #0000ff;">from rest_framework.renderers <span style="color: #0000ff;">import<span style="color: #000000;"> AdminRenderer
<span style="color: #0000ff;">from rest_framework.renderers <span style="color: #0000ff;">import<span style="color: #000000;"> HTMLFormRenderer
<span style="color: #0000ff;">from .. <span style="color: #0000ff;">import<span style="color: #000000;"> models
<span style="color: #0000ff;">class<span style="color: #000000;"> TestSerializer(serializers.ModelSerializer):
<span style="color: #0000ff;">class<span style="color: #000000;"> Meta:
model =<span style="color: #000000;"> models.UserInfo
fields = <span style="color: #800000;">"<span style="color: #800000;">all<span style="color: #800000;">"
<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
renderer_classes =<span style="color: #000000;"> [HTMLFormRenderer,**<span style="color: #000000;">kwargs):
user_list =<span style="color: #000000;"> models.UserInfo.objects.all().first()
ser = TestSerializer(instance=user_list,many=<span style="color: #000000;">False)
<span style="color: #0000ff;">return Response(ser.data)
4. 自定义显示模板
访问URL:
@H_404_510@http://127.0.0.1:8000/test/?format=html
@H_404_510@http://127.0.0.1:8000/test.html
@H_404_510@http://127.0.0.1:8000/test/
django.conf.urls
rest_framework.views rest_framework.response rest_framework rest_framework.renderers <span style="color: #0000ff;">from .. <span style="color: #0000ff;">import<span style="color: #000000;"> models
<span style="color: #0000ff;">class<span style="color: #000000;"> TestSerializer(serializers.ModelSerializer):
<span style="color: #0000ff;">class<span style="color: #000000;"> Meta:
model =<span style="color: #000000;"> models.UserInfo
fields = <span style="color: #800000;">"<span style="color: #800000;">all<span style="color: #800000;">"
<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
renderer_classes =<span style="color: #000000;"> [TemplateHTMLRenderer,many=<span style="color: #000000;">False)
<span style="color: #0000ff;">return Response(ser.data,template_name=<span style="color: #800000;">'<span style="color: #800000;">user_detail.html<span style="color: #800000;">')
Meta
Title
5. 浏览器格式API+JSON
访问URL:
@H_404_510@http://127.0.0.1:8000/test/?format=api
@H_404_510@http://127.0.0.1:8000/test.api
@H_404_510@http://127.0.0.1:8000/test/
rest_framework.views rest_framework.response rest_framework <span style="color: #0000ff;">from rest_framework.renderers <span style="color: #0000ff;">import<span style="color: #000000;"> JSONRenderer
<span style="color: #0000ff;">from rest_framework.renderers <span style="color: #0000ff;">import<span style="color: #000000;"> BrowsableAPIRenderer
<span style="color: #0000ff;">from .. <span style="color: #0000ff;">import<span style="color: #000000;"> models
<span style="color: #0000ff;">class<span style="color: #000000;"> TestSerializer(serializers.ModelSerializer):
<span style="color: #0000ff;">class<span style="color: #000000;"> Meta:
model =<span style="color: #000000;"> models.UserInfo
fields = <span style="color: #800000;">"<span style="color: #800000;">all<span style="color: #800000;">"
<span style="color: #0000ff;">class<span style="color: #000000;"> CustomBrowsableAPIRenderer(BrowsableAPIRenderer):
<span style="color: #0000ff;">def<span style="color: #000000;"> get_default_renderer(self,view):
<span style="color: #0000ff;">return<span style="color: #000000;"> JSONRenderer()
<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
renderer_classes =<span style="color: #000000;"> [CustomBrowsableAPIRenderer,template_name=<span style="color: #800000;">'<span style="color: #800000;">user_detail.html<span style="color: #800000;">')
注意:如果同时多个存在时,自动根据URL后缀来选择渲染器。