2017-10-15 261 views
0

我正在通過此Getting started with Django Rest Framework by Building a Simple Product Inventory Manager教程工作。在最後的教程中,它說我「現在應該能夠運行你的服務器並開始使用不同的API端點」。但是,當我運行服務器時,我得到的是一個TemplateDoesNotExist錯誤。在教程中沒有提到創建模板(這最終將連接到Angular 2前端,如this tutorial所示),所以我很困惑這是否是我的代碼中的錯誤,或者如果教程離開了一步。當我運行我的代碼時,我沒有遇到任何控制檯錯誤。django - TemplateDoesNotExist錯誤

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

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 

如果你需要看到更多的我的代碼,請評論,我會發布,但一切都應該與教程中給出的代碼相同。

回答

0

可能會忘記在已安裝的應用程序(settings.py)中添加「rest_framework」。

INSTALLED_APPS = (
    ... 
    'rest_framework', 
) 

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

+0

改變了錯誤我得到,至少。現在它顯示「OperationalError at/products/no such table:inventory_product」。我認爲這僅僅意味着我需要向表中添加數據? – rustbird

+0

不可以。您已在數據庫級別首先創建表。 –

+0

使用「python manage.py makemigrations」和「python manage.py migrate」 –

相關問題