2016-04-15 110 views
0

我的觀點看起來像以下未發現:Django的模板意見

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) 

這個視圖返回我JSON數據通過API直接訪問時(即直接擊中網址在瀏覽器中)。當我通過角訪問它,我得到如下回應:Object {data: Array[2], status: 200, config: Object, statusText: "OK"}

更改JSONResponse(serializer.data)return Response(serializer.data)給我TemplateNotFound錯誤。然而,類似的代碼正在我的另一個項目中使用相同的設置,成功返回JSON和角度我得到[object1,object2]。

可能是什麼原因?

回答

0

您的INSTALLED_APPSsettings.py文件中有'rest_framework',嗎?