Coe*_*eus 37 django django-views django-rest-framework
在我的视图函数中,我想返回一个json对象(data1)和一些text/html(form).这可能吗?
我的代码
@api_view(['POST'])
@permission_classes((AllowAny,))
def create_user(request):
if request.is_ajax():
if request.method == 'POST':
serializer = SignupSerializer(data=request.data)
print 'ser'
print serializer
if not serializer.is_valid():
return Response(serializer.errors,\
status=status.HTTP_400_BAD_REQUEST)
else:
serializer.save()
data={'status': 'Created','message': 'Verification email has been sent to your email. Please verify your account.'}
return Response(data, template_name='register.html')
else:
return HttpResponse('hello world')
Run Code Online (Sandbox Code Playgroud)
当我调用url时,我得到状态代码500,错误如下所示
TemplateDoesNotExist rest_framework/api.html
当我作为API检查时,我得到200 ok状态的响应.这表明我无法获取我的HTML页面
我应该如何根据要求获取我的HTML
提前致谢
Mar*_*ner 21
This is my attempt to explain the problem and collect everyone else's responses into a single list. Thanks to everyone for giving me shoulders to stand on!
I believe this happens because Django REST Framework wants to render a template (rest_framework/api.html), but the template can't be found. It seems there are two approaches to fix this:
Ensure REST Framework is included in INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
...
'rest_framework',
...
]
Run Code Online (Sandbox Code Playgroud)
And ensure APP_DIRS is True in your template configuration (it defaults to False if not specified). This allows Django to look for templates in installed applications. Here's a minimal configuration that seems to work, though you might have more config here:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
},
]
Run Code Online (Sandbox Code Playgroud)
If you tell REST Framework to render a JSON response then it doesn't need to use a template so you don't need to change the APP_DIRS settings as mentioned above. It also seems like you might not even need to list rest_framework in INSTALLED_APPS, though it might be necessary to have it there for other reasons.
You can do this globally in settings.py:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
]
}
Run Code Online (Sandbox Code Playgroud)
Or if you're using the @api_view decorator then you can specify JSONRenderer on each view:
@api_view(['GET'])
@renderer_classes([JSONRenderer])
def some_view(request):
return Response({'status': 'yay'})
Run Code Online (Sandbox Code Playgroud)
See the REST Framework renderers documentation for details.
小智 17
确保您安装了pip install djangorestframeworkrest_framework并将其包含在setting.py中
INSTALLED_APPS = [
'rest_framework',
]
Run Code Online (Sandbox Code Playgroud)
p k*_*aha 15
''' 尝试其中之一肯定会对您有所帮助:
1:将rest_framework添加到settings.py应用程序列表中,有时您的应用程序列出的顺序可能是原因。'''
INSTALLED_APPS = [
'rest_framework',
...
]
Run Code Online (Sandbox Code Playgroud)
''' 2:检查您的模板设置。请参阅后端、DIR 和 APP_DIRS。如果您自定义了rest-framework模板,请检查您定义的路径是否正确,设置 APP_DIRS: True 。在大多数情况下,这将得到解决。'''
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR, os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
....
}
]
Run Code Online (Sandbox Code Playgroud)
''' 3:检查默认渲染器类设置:'''
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
...
]
}
Run Code Online (Sandbox Code Playgroud)
''' 4:如果您在基于函数的视图上使用 api_view 装饰器,请确保您已正确提供渲染器。IE
@renderer_classes([JSONRenderer]) '''
小智 14
确保您将以下内容添加到rest_framework您的文件中:installed_appssettings.py
INSTALLED_APPS = [
'rest_framework',
]
Run Code Online (Sandbox Code Playgroud)
Ros*_*ers 11
我从旧的Django版本升级到Django 2.0时遇到了这个问题.我settings.py根本没有TEMPLATE指令,所以我从新的django-admin.py startproject ...运行中抓住了以下内容:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': 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',
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
settings.py如果您没有TEMPLATES指令,请将其添加到您的settings.py.为我工作.
| 归档时间: |
|
| 查看次数: |
15901 次 |
| 最近记录: |