【发布时间】:2014-02-19 21:56:02
【问题描述】:
我正在使用 Django Rest 框架。我不断收到错误
Exception Type: TemplateDoesNotExist
Exception Value: rest_framework/api.html
我不知道我怎么会出错。这是我第一次尝试 REST Framework。 这是代码。
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'),
)
【问题讨论】:
-
您的 settings.py INSTALLED_APPS 中是否列出了“rest_framework”?
-
Noobest 错误。谢谢你。
-
我还有一个疑问。我如何获得一个表格,以便我在表格中发布一些数据,用这些值访问数据库并获得结果?
How do I get the form? -
@ScottWoodall 请发布答案,并获得一些积分!
标签: python django django-rest-framework