TemplateDoesNotExist - Django error

I am using the Django Rest Framework. and I keep getting the error message

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

I donโ€™t know how wrong I am. This is the first time I'm trying to figure out the REST Framework. This is the code.

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

from django.forms import widgets from rest_framework import serializers from modules.data.models import *

 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'), ) 
+102
python django django-rest-framework
Jan 28 '14 at 14:29
source share
6 answers

Make sure you have the rest_framework specified in your settings.py INSTALLED_APPS .

+249
Jan 31 '14 at 14:27
source share

For me, rest_framework/api.html actually missing from the file system due to a corrupt installation or some other unknown reason. Reinstalling djangorestframework fixed the problem:

 $ pip install --upgrade djangorestframework 
+5
Dec 21 '14 at 19:18
source share

Please note that DRF is trying to return data in the same format that was requested. From your browser, this is most likely HTML. To specify an alternative answer, use the ?format= parameter. For example ?format=json .

The TemplateDoesNotExist error occurs most often when you visit the API endpoint in your browser, and you do not have rest_framework included in the list of installed applications, as described by other respondents.

If you do not have DRF included in your application list but donโ€™t want to use the HTML Admin DRF page, try using an alternative format for the โ€œside stepโ€ of this error message.

Further information from the documentation: http://www.django-rest-framework.org/topics/browsable-api/#formats

+3
Oct 02 '15 at 21:07
source share

Itโ€™s none of your business, but a possible reason is setting loaders for Django . For example, if you have settings (with 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 search for application folders with templates, because for this you need to explicitly add django.template.loaders.app_directories.Loader to loaders .

Note that by default, django.template.loaders.app_directories.Loader included in loaders .

+3
Apr 13 '16 at 16:48
source share

I came across the same error message. In my case, this was due to setting up the backend for Jinja2. In my settings file:

 TEMPLATES = [ { 'BACKEND': 'django.template.backends.jinja2.Jinja2', ... 

Changing this default value 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.

0
Sep 14 '18 at 21:47
source share

Sometimes there is no rest_framework in your project
Install rest-framework: pip install djangorestframework and pip install djangorestframework rest of the frameworks in setting.py: INSTALLED_APPS = [

 'rest_framework', 

]

0
Feb 06 '19 at 9:45
source share



All Articles