【问题标题】:ValueError: The view didn't return an HttpResponse object. It returned None instead. (Django/ImgKit)ValueError:视图未返回 HttpResponse 对象。它返回 None 代替。 (Django/ImgKit)
【发布时间】:2021-04-28 04:16:56
【问题描述】:

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)

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

【问题讨论】:

  • 当您的ifs 或您的try 子句之一失败时,您不会返回HttpResponse 对象。

标签: javascript python django django-views imgkit


【解决方案1】:

你没有返回任何东西:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2021-09-01
    • 2019-06-12
    • 2018-06-02
    • 2020-08-19
    • 2020-08-09
    • 2021-12-10
    相关资源
    最近更新 更多