-1

ValueError: The view didn't return an HttpResponse object. It returned None instead.(Django/ImgKit)

我正在尝试使用 fetch api 将用户在前端选择的内容连接到后端,然后返回 json 响应。对于他们选择的任何 html 文件,我想将其转换为要显示的图像以供他们预览

不知道为什么我会遇到这个错误:

def folder_view(request):
    if request.method == 'POST':
        if json.loads(request.body)['template'] == 'template':
            try:
                folder = json.loads(request.body)['folder']
                templates = Template.objects.filter(user=request.user, folder=folder).values('template_name')
                user_templates=[]
                for template in templates:
                    config = imgkit.config(wkhtmltoimage=WKHTMLTOPDF_PATH, xvfb='/usr/bin/xvfb-run')
                    html_img = imgkit.from_url(template.html.url, False, config=config)
                    user_templates.append(html_img)     
                return JsonResponse({'user_templates': user_templates })
            except Exception as e:
                print('test')

错误似乎来自这一行:html_img = imgkit.from_url(template.html.url, False, config=config)

删除此行后,将不再出现错误

4

1 回答 1

1

你没有返回任何东西:

def folder_view(request):
    if request.method == 'POST':
         .........
         ....
    return render(request,your_template.html,context) #add here
于 2021-04-28T04:28:00.507 回答