我在Django和LogicalDOC之间的API Rest Python遇到了一个新问题。
我正在LogicalDOC中创建一个文件夹,然后我想把我的pdf文件保存在这个新文件夹中folderId
。
但是,当它看似有效时,因为从我的观点来看语法很好:没有pdf文件出现。
我创建了一个文件夹,我使用命令data["id"]
获取了他的ID号:348930,当我想将我的pdf文件保存在str(data["id"])
时,我在FolderId
中插入@login_required
def BirthCertificate_PDF(request, id) :
birthcertificate = get_object_or_404(BirthCertificate, pk=id)
data = {"birthcertificate" : birthcertificate}
template = get_template('BC_raw.html')
html = template.render(Context(data))
filename_directory = str(BirthCertificate.objects.get(pk=id).lastname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).firstname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).birthday)
filename = 'Acte_Naissance_' + filename_directory + '.pdf'
path = '/Users/valentinjungbluth/Desktop/Django/Individus/' + filename
file = open(path, "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')
file.seek(0)
pdf = file.read()
if pdf :
payload = '{{ "name":"{0}", "parentId":3309569 }}'.format(filename_directory) #Fix parent folder
url = 'http://localhost:8080/services/rest/folder/create'
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
resp = requests.post(url, data=payload, headers=headers, auth=('admin', 'admin'))
rbody = resp.content
data = json.loads(rbody)
print data["id"] #Get ID from the new folder
payload = '{{ "language":"fr","fileName":"{0}","FolderId":'+str(data["id"]) +'}}'.format(filename) #save pdf file inside the new folder thanks to his ID
upfile = path
files = {
'document': (None, payload, 'application/json'),
'content': (os.path.basename(upfile), open(upfile, 'rb'), 'application/octet-stream')
}
url = 'http://localhost:8080/services/rest/document/create'
headers = {'Content-Type': 'multipart/form-data'}
r = requests.post(url, files=files, headers=headers, auth=('admin', 'admin'))
context = {"birthcertificate":birthcertificate,
"path":path}
return render(request, 'BC_PDF.html', context)
file.close()
return HttpResponse(pdf, 'application/pdf')
新文件夹。
新文件夹已创建且运行良好,但pdf文件未保存在内部。有什么问题?
这是我的剧本:
data["id"]
这是一个屏幕截图,显示folderID应为:3538970 这个数字也由:{{1}}
给出答案 0 :(得分:1)
正如我在评论中所说,您不需要使用字符串连接将FolderId作为有效负载传递,只需使用format方法的第二个参数:
d = '{{ "language":"fr","fileName":"{0}","FolderId":"{1}"}}'.format(filename, str(data["id"]))