Django模板文档在/ tmp /中创建tmp文件

时间:2017-09-20 10:02:04

标签: python django

您好我正在使用django templated-docs生成pdf发票,但挑战是它在/ tmp /(ubuntu的根目录)中生成文件我希望它在django目录中生成文件这里是我的代码:

"""
    Mail Receipt
    """
    ctx = {
        "customer_names": receipt.customer_full_name,
        "customer_phone": receipt.phone_number,
        'date_assessed': receipt.date_time.strftime('%d-%m-%Y %H:%M'),
        'grand_total': gross,
        "amount": total,
        "invoice_number": receipt.invoice_number,
        "smart_card_no": receipt.smart_card_number,
        "receipt_signature": response['signature'],
    }
    print "***********Generating Pdf Invoice now"
    filename = fill_template(template_name='email_templates/invoice.ods', output_format='pdf', context=ctx)
    print '************************** Finished generating file *******************'
    visible_filename = 'invoice.{}'.format('pdf')

    f = FileResponse(filename, visible_filename)
    # print 'Tmp File name {}'.format(f)
    print f
    # fs = FileSystemStorage(f)
    # filename = fs.save(filename, f)
    # invoice = os.path.join('files/' + filename)
    import shutil
    import os

    source = os.listdir("/tmp/")
    destination = os.path.join('files/invoice_{}.pdf'.format(receipt.invoice_number))
    for files in source:
        if files.endswith(".pdf"):
            shutil.move(files, destination)

    # print 'Invoice Generated {}'.format(invoice)
    #
    msg = EmailMessage("Sales Receipt", "Receipt", 'user@server.com', [receipt.email_address])
    msg.attach_file(destination)
    msg.send(fail_silently=True)
    print 'Sent Invoice'

任何人都知道如何做到这一点,

1 个答案:

答案 0 :(得分:1)

您将来源视为:

source = os.listdir("/tmp/")

将source作为django目录的绝对路径。