我想创建一个以json格式显示模型内容的页面。 现在我收到了一个错误, 在/ app / api / get的TemplateDoesNotExist rest_framework / api.html。 models.py是
from django.db import models
# Create your models here.
class Color(models.Model):
name = models.CharField(max_length=255)
background_color = models.CharField(max_length=255)
h1_color = models.CharField(max_length=255)
p_color = models.CharField(max_length=255)
def __str__(self):
return self.name
serializers.py是
from .models import Color
from rest_framework import serializers
class TaskSerializer(serializers.Serializer):
task_name = serializers.CharField(max_length=100,source='name')
class Meta:
model = Color
fields = ('name',
'background_color',
'h1_color',
'p_color',
'task_name')
urls.py是
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'api/get',views.TaskGet.as_view(),name='task-get')
]
views.py是
from django.shortcuts import render
from .models import Color
from .forms import ColorForm
from .serializers import TaskSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
# Create your views here.
def index(request):
d = {
'colors': Color.objects.all(),
'form': ColorForm(),
}
return render(request, 'index.html', d)
class TaskGet(APIView):
def get(self, request, format=None):
obj = Color.objects.all()
serializers = TaskSerializer(obj, many=True)
return Response(serializers.data, status.HTTP_200_OK)
我没有在任何地方写过api.html。我没有注册frest_framework目录。所以,我真的不明白为什么会发生这个错误。我怎么能解决这个问题?
追溯是
Traceback:
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
217. response = self.process_exception_by_middleware(e, request)
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
215. response = response.render()
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/django/template/response.py" in render
107. self.content = self.rendered_content
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/rest_framework/response.py" in rendered_content
72. ret = renderer.render(self.data, accepted_media_type, context)
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/rest_framework/renderers.py" in render
705. template = loader.get_template(self.template)
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/django/template/loader.py" in get_template
25. raise TemplateDoesNotExist(template_name, chain=chain)
Exception Type: TemplateDoesNotExist at /app/api/get/
Exception Value: rest_framework/api.html
答案 0 :(得分:1)
确保rest_framework
中的INSTALLED_APPS
列表中列出了settings.py
:
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework' # <<<
]
首先,更改您的task-get
网址格式正则表达式:
from django.conf.urls import url
from . import views
urlpatterns = [
# ...
url(r'^api/get/$', views.TaskGet.as_view(), name='task-get')
# ____^________^___________________
# Put "begin" and "end" of the url.
]
由于您要显示所有Color
,因此使用 DRF 标准库中的ListAPIView
可能是更好的方法:
from rest_framework import generics
class TaskGet(generics.ListAPIView):
serializer_class = TaskSerializer
queryset = Color.objects.all()
如您所见,不需要实施get
方法,因为这是标准GET
请求,您只需指定queryset
和{{1这将序列化这个集合。 DRF 会关心其余部分。
此方法将列出数据库中的所有serializer_class
条目,并以JSON格式序列化。