ValueError:视图未返回 HttpResponse 对象它返回 None (Django/ImgKit)

问题描述

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,config=config)

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

解决方法

你没有返回任何东西:

def folder_view(request):
    if request.method == 'POST':
         .........
         ....
    return render(request,your_template.html,context) #add here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...