使用restframework 报错
django.template.exceptions.TemplateDoesNotExist: rest_framework/api.html
class MyAuthentication(object):
def authenticate(self, request):
token = request._request.GET.get('token')
if not token:
# 此句报错
raise exceptions.AuthenticationFailed('用户认证失败')
return ('alex', None)
def authenticate_header(self, val):
pass
报错信息如下:
原因有两个:
- 在 settings.py 中注册rest_framework
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'index', 'rest_framework', # 在此处注册 app ]
- settings.py 中的 TEMPLATES中的 APP_DIRS 设置为 True
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, # 设置为 True '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', ], }, }, ]
报错解决