简体   繁体   English

django-TemplateDoesNotExist错误

[英]django - TemplateDoesNotExist error

I am working through this Getting started with Django Rest Framework by Building a Simple Product Inventory Manager tutorial. 我正在通过构建简单产品库存管理器教程来学习Django Rest Framework入门 At the end the tutorial, it says that I "should now be able to run your server and start playing with diffrent API endpoints". 在教程的最后,它说我“现在应该能够运行您的服务器并开始使用不同的API端点”。 However, when I run the server, all I'm getting is a TemplateDoesNotExist error. 但是,当我运行服务器时,我得到的只是一个TemplateDoesNotExist错误。 At no point in the tutorial does it mention creating templates (and this is eventually going to connect to an Angular 2 frontend, as shown in this tutorial ), so I'm confused at to whether this is an error in my code, or if the tutorial left a step out. 在本教程中的任何时候都没有提到创建模板(最终将连接到Angular 2前端,如本教程中所示),因此我对于这是否是我的代码中的错误感到困惑,或者本教程走了一步。 I do not get any console errors when I run my code. 运行代码时,我没有任何控制台错误。

serializers.py serializers.py

from .models import Product, Family, Location, Transaction
from rest_framework import serializers

class LocationSerializer(serializers.ModelSerializer):
  class Meta:
    model = Location
    fields = ('reference', 'title', 'description')

class FamilySerializer(serializers.ModelSerializer):
  class Meta:
    model = Family
    fields = ('reference', 'title', 'description', 'unit', 'minQuantity')

class ProductSerializer(serializers.HyperlinkedModelSerializer):
  class Meta:
    model = Product
    fields = ('sku', 'barcode', 'title', 'description', 'location', 'family')
    depth = 1

class TransactionSerializer(serializers.ModelSerializer):
  product = ProductSerializer()
  class Meta:
    model = Transaction
    fields = ('sku', 'barcode', 'product')

views.py views.py

from __future__ import unicode_literals

from django.shortcuts import render

from rest_framework import status, generics, mixins
from rest_framework.decorators import api_view
from rest_framework.response import Response

from .models import Product, Location, Family, Transaction
from .serializers import *

# Create your views here.

@api_view(['GET', 'POST'])
def product_list(request):
    """
    List all products, or create a new product.
    """
    if request.method == 'GET':
        products = Product.objects.all()
        serializer = ProductSerializer(products,context={'request': request} ,many=True)
        return Response(serializer.data)
    elif request.method == 'POST':
        serializer = ProductSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT', 'DELETE'])
def product_detail(request, pk):
    """
    Retrieve, update or delete a product instance.
    """
    try:
        product = Product.objects.get(pk=pk)
    except Product.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ProductSerializer(product,context={'request': request})
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = ProductSerializer(product, data=request.data,context={'request': request})
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        product.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

class family_list(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
    queryset = Family.objects.all()
    serializer_class = FamilySerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

class family_detail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Family.objects.all()
    serializer_class = FamilySerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

class location_list(generics.ListCreateAPIView):
    queryset = Location.objects.all()
    serializer_class = LocationSerializer

class location_detail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Location.objects.all()
    serializer_class =  LocationSerializer

class transaction_list(generics.ListCreateAPIView):
    queryset = Transaction.objects.all()
    serializer_class = TransactionSerializer

class transaction_detail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Transaction.objects.all()
    serializer_class =  TransactionSerializer

IF you need to see more of my code, please comment and I'll post it, but everything should be identical to the code given in the tutorial. 如果您需要查看更多代码,请发表评论并将其发布,但是所有内容都应与本教程中给出的代码相同。

May be you could forget to add "rest_framework" in installed apps(settings.py). 可能是您忘记了在已安装的应用程序(settings.py)中添加“ rest_framework”。

INSTALLED_APPS = (
    ...
    'rest_framework',
)

http://www.django-rest-framework.org/#installation http://www.django-rest-framework.org/#installation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM