我上了这个课:
class TechSolutionDetail(APIView):
renderer_classes = (TemplateHTMLRenderer,)
def get(self, request, solution_name=None, format=None):
techSolution = get_object_or_404(TechSolution, title=djangology_unquote(solution_name), deleted=False)
return Response({'solution': techSolution},
template_name='core2/solution.html')
# Update a techSolution
@method_decorator(login_required)
def post(self, request, solution_id, format=None):
techSolution = get_object_or_404(TechSolution, pk=solution_id)
serializer = TechSolutionsSerializer(techSolution, data=request.data)
if serializer.is_valid():
return redirect(techSolution.issue.get_view_link())
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
在get
函数中,我返回了一个自定义html模板,因此需要定义renderer_classes = (TemplateHTMLRenderer,)
。
在post
函数上,如果serializer.is_valid()
函数失败,则应返回由Django Rest Framework(DRF)生成的包含验证错误的模板(在行return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
上)。 / p>
问题是:如果我定义了renderer_classes = (TemplateHTMLRenderer,)
函数所需的行get
,则在推子失败时会返回:
Returned a template response with no `template_name` attribute set on either the view or response
似乎我需要在template_name=
方法的return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
行上定义post
,但找不到模板的定义方式或方式。