Python TemplateDoesNotExist - Django 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21408344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TemplateDoesNotExist - Django Error
提问by python-coder
I'm using Django Rest Framework. and I keep getting an error
我正在使用 Django Rest 框架。我不断收到错误消息
Exception Type: TemplateDoesNotExist
Exception Value: rest_framework/api.html
I dont know how I'm going wrong. This is the first time I'm trying out hands on REST Framework. This is code.
我不知道我怎么了。这是我第一次尝试使用 REST 框架。这是代码。
views.py
视图.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
模型.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
序列化程序.py
from django.forms import widgets from rest_framework import serializers from modules.data.models import *
从 django.forms 导入小部件 从 rest_framework 导入序列化器 从 modules.data.models 导入 *
class ActionSerializer(serializers.ModelSerializer):
class Meta:
model = Controller
fields = ('id', 'name', 'ip', 'installation_id')
urls.py
网址.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'),
)
采纳答案by Scott Woodall
Make sure you have rest_frameworklisted in your settings.pyINSTALLED_APPS.
确保您已rest_framework在您的settings.pyINSTALLED_APPS.
回答by Jace Browning
For me, rest_framework/api.htmlwas actually missing on the filesystem due to a corrupt installation or some other unknown reason. Reinstalling djangorestframeworkfixed the problem:
对我来说,rest_framework/api.html由于安装损坏或其他一些未知原因,文件系统中实际上丢失了。重新安装djangorestframework解决了问题:
$ pip install --upgrade djangorestframework
回答by TheCatParty
Please note that the DRF attempts to return data in the same format that was requested. From your browser, this is most likely HTML. To specify an alternative response, use the ?format=parameter. For example: ?format=json.
请注意,DRF 尝试以与请求的格式相同的格式返回数据。从您的浏览器来看,这很可能是 HTML。要指定替代响应,请使用?format=参数。例如:?format=json。
The TemplateDoesNotExisterror occurs most commonly when you are visiting an API endpoint in your browser and you do nothave the rest_frameworkincluded in your list of installed apps, as described by other respondents.
在TemplateDoesNotExist当您访问在浏览器的API端点发生最常见的错误,你就不能有rest_framework包含在您安装的应用程序列表,其他受访者描述。
If you do not have DRF included in your list of apps, but don't want to use the HTML Admin DRF page, try using an alternative format to 'side-step' this error message.
如果您的应用程序列表中没有包含 DRF,但不想使用 HTML 管理 DRF 页面,请尝试使用替代格式来“回避”此错误消息。
More info from the docs here: http://www.django-rest-framework.org/topics/browsable-api/#formats
来自此处文档的更多信息:http: //www.django-rest-framework.org/topics/browsable-api/#formats
回答by prokaktus
Not your case, but also possible reason is customized loadersfor Django. For example, if you have in settings (since Django 1.8):
不是您的情况,但也可能是loaders为Django. 例如,如果您在设置中(因为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 will not try to look at applications folders with templates, because you should explicitly add django.template.loaders.app_directories.Loaderinto loadersfor that.
Django 不会尝试查看带有模板的应用程序文件夹,因为您应该明确地添加django.template.loaders.app_directories.Loader进去loaders。
Notice, that by default django.template.loaders.app_directories.Loaderincluded into loaders.
请注意,默认情况下django.template.loaders.app_directories.Loader包含在loaders.
回答by calico_
I ran into the same error message. In my case, it was due to setting the backend to Jinja2. In my settings file:
我遇到了同样的错误消息。就我而言,这是由于将后端设置为 Jinja2。在我的设置文件中:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
...
Changing this back to the default fixed the problem:
将其改回默认值解决了问题:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
...
Still not sure if there is a way to use the Jinja2 backend with rest_framework.
仍然不确定是否有办法将 Jinja2 后端与 rest_framework 一起使用。

