Django—将使用xhtml2pdf创建的PDF写入服务器';s盘

Django—将使用xhtml2pdf创建的PDF写入服务器';s盘,django,xhtml2pdf,Django,Xhtml2pdf,我正在尝试编写一个脚本,将xhtml2pdf创建的pdf直接保存到服务器,而不执行通常的提示用户将其下载到计算机的路径。Documents()是我试图保存到的模型,新的项目和输出文件名变量在别处设置 html = render_to_string(template, RequestContext(request, context)).encode('utf8') result = open(output_filename, "wb")

我正在尝试编写一个脚本,将xhtml2pdf创建的pdf直接保存到服务器,而不执行通常的提示用户将其下载到计算机的路径。Documents()是我试图保存到的模型,新的项目和输出文件名变量在别处设置

            html = render_to_string(template, RequestContext(request, context)).encode('utf8')
            result = open(output_filename, "wb")
            pdf = CreatePDF(src=html, dest=results, path = "", encoding = 'UTF-8', link_callback=link_callback) #link callback was originally set to link_callback, defined below
            result.close()
            if not pdf.err:

                new_doc=Documents()
                new_doc.project=new_project
                new_doc.posted_by=old_mess[0].from_user_fk.username
                new_doc.documents = result
                new_doc.save()
使用此配置,当它到达new_doc.save()时,我得到一个错误:“file”对象没有属性“\u committed”


有人知道我怎么解决这个问题吗?谢谢

玩过之后,我找到了一个有效的解决方案。问题是,当结果(pdf)仍然打开时,我没有创建新文档

需要将“+”添加到open()中,以便pdf文件可以读写,而不仅仅是写

请注意,这会首先将pdf保存在不同的文件夹(文件)中。如果这不是您的应用程序所需的结果,则需要将其删除

            html = render_to_string(template, RequestContext(request, context)).encode('utf8')
            results = StringIO()
            result = open("Files/"+output_filename, "w+b")
            pdf = CreatePDF(src=html, dest=results, path = "", encoding = 'UTF-8', link_callback=link_callback) #link callback was originally set to link_callback, defined below

            if not pdf.err:
                result.write(results.getvalue())
                new_doc=Documents()
                new_doc.project=new_project
                new_doc.documents.save(output_filename, File(result))
                new_doc.posted_by=old_mess[0].from_user_fk.username

                new_doc.save()
            result.close()