2014-01-28 252 views
72

我正在使用Django Rest Framework。和我不斷收到錯誤TemplateDoesNotExist - Django錯誤

Exception Type: TemplateDoesNotExist 
Exception Value: rest_framework/api.html 

我不知道我怎麼會出錯。這是我第一次嘗試使用REST框架。 這是代碼。

views.py

import socket, json 
from modules.data.models import * 
from modules.utils import * 
from rest_framework import status 
from rest_framework.decorators import api_view 
from rest_framework.response import Response 
from modules.actions.serializers import ActionSerializer 


@api_view(['POST']) 
@check_field_exists_wrapper("installation") 
def api_actions(request, format = None): 

    action_type = request.POST['action_type'] 
    if action_type == "Shutdown" : 
     send_message = '1' 
     print "Shutting Down the system..." 
    elif action_type == "Enable" : 
     send_message = '1' 
     print "Enabling the system..." 
    elif action_type == "Disable" : 
     send_message = '1' 
     print "Disabling the system..." 
    elif action_type == "Restart" : 
     send_message = '1' 
     print "Restarting the system..." 

    if action_type in ["Shutdown", "Enable", "Disable"] : PORT = 6000 
    else : PORT = 6100 

    controllers_list = Controller.objects.filter(installation_id = kwargs['installation_id']) 

    for controller_obj in controllers_list: 
     ip = controller_obj.ip 
     try: 
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
      s.connect((ip, PORT)) 
      s.send(send_message) 
      s.close() 
     except Exception as e: 
      print("Exception when sending " + action_type +" command: "+str(e)) 

    return Response(status = status.HTTP_200_OK) 

models.py

class Controller(models.Model): 
    id = models.IntegerField(primary_key = True) 
    name = models.CharField(max_length = 255, unique = True) 
    ip = models.CharField(max_length = 255, unique = True) 
    installation_id = models.ForeignKey('Installation') 

serializers.py

從django.forms匯入rest_framework進口串行部件從modules.data.models導入*

class ActionSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = Controller 
     fields = ('id', 'name', 'ip', 'installation_id') 

urls.py

from django.conf.urls import patterns, url 
from rest_framework.urlpatterns import format_suffix_patterns 

urlpatterns = patterns('modules.actions.views', 
    url(r'^$','api_actions',name='api_actions'), 
) 
+23

你有沒有在你的settings.py INSTALLED_APPS中列出「rest_framework」? –

+0

Noobest錯誤。感謝它。 –

+0

我還有一個疑問。我如何得到一個表單,以便我在表單中發佈一些數據,用這些值打到數據庫並獲得結果? '我如何獲得表格?' –

回答

186

確保您已rest_frameworksettings.pyINSTALLED_APPS上市。

+0

也可能是因爲未註冊DRF的依賴關係。在我的情況下,我不得不將''django_hstore','添加到'INSTALLED_APPS'。 – shacker

5

對我來說,rest_framework/api.html實際上在文件系統上由於損壞的安裝或其他未知原因而丟失。重新安裝djangorestframework解決了這一問題:

$ pip install --upgrade djangorestframework 
+1

同樣的事情發生在我身上。請注意,該命令將升級到可能不兼容的版本(即使您的setup.py和/或requirements.txt另有說明)。你可以嘗試點卸載/安裝或指定確切的版本。 –

3

請注意,DRF試圖在被請求相同的格式返回數據。在你的瀏覽器中,這很可能是HTML。要指定替代響應,請使用?format=參數。例如:​​。

當您訪問在瀏覽器的API端點和你有包含在安裝的應用程序列表中的rest_framework,由其他受訪者描述發生的錯誤TemplateDoesNotExist最常見的。

如果您的應用程序列表中沒有包含DRF,但不想使用HTML Admin DRF頁面,請嘗試使用其他格式來「側移」此錯誤消息。

從這裏的文檔更多信息:http://www.django-rest-framework.org/topics/browsable-api/#formats

3

不是你的情況下,也有可能的原因是爲Django定製loaders。例如,如果你在設置(因爲Django 1.8):

TEMPLATES = [ 
{ 
    ... 
    'OPTIONS': { 
     'context_processors': [ 
      'django.template.context_processors.debug', 
      'django.template.context_processors.request', 
      'django.contrib.auth.context_processors.auth', 
      'django.contrib.messages.context_processors.messages' 
     ], 
     'loaders': [ 
      'django.template.loaders.filesystem.Loader', 
     ], 
     ... 
    } 
}] 

Django將不嘗試一下使用模板的應用程序文件夾,因爲你應該明確地添加到django.template.loaders.app_directories.Loaderloaders那個。

請注意,默認django.template.loaders.app_directories.Loader包含在loaders中。

+0

警告: ?:(1_8.W001)獨立的TEMPLATE_ *設置在Django 1.8中已棄用,並且TEMPLATES字典優先。您必須將以下設置的值放入您的默認模板字典:TEMPLATE_DEBUG。 – andi