我的观点如下:
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from flights.models import Farmer
from flights.serializers import FarmerSerializer
from django.contrib.auth.models import User
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.decorators import api_view
import datetime
class JSONResponse(HttpResponse):
"""
An HttpResponse that renders its content into JSON.
"""
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
#view to get the details of all the farmers
@api_view(('GET',))
def get_all_farmers_details(request):
#Fetch all the farmers from database
results = Farmer.objects.all()
#Serialize the obtained results
serializer = FarmerSerializer(results, many=True)
return JSONResponse(serializer.data)
当通过api直接访问时(即直接在浏览器中访问url),此视图返回JSON数据。当我通过角度访问它时,我得到以下回复:Object {data: Array[2], status: 200, config: Object, statusText: "OK"}
将JSONResponse(serializer.data)
更改为return Response(serializer.data)
会给我 TemplateNotFound 错误。但是类似的代码在我的另一个项目中使用相同的设置成功返回JSON,并且在角度我得到[object1,object2]。
可能是什么原因?
答案 0 :(得分:0)
'rest_framework',
INSTALLED_APPS
文件中的settings.py
是否有 <p class="input-group">
<input type="text" class="form-control" uib-datepicker-popup ng-model="dt" is-open="popup2.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open2()"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
?