Search code examples
pythondjangodjango-modelsdjango-staticfilespypdf

Customizing output file name


with open('media_cdn/account/pdf01.pdf', 'wb') as out_file:
        output.write(out_file)

How can I format this to have a custom name for each output? For example, I have tried:

author_id=str(instance.author.id)

with open('media_cdn/account/{author_id}-pdf01.pdf', 'wb') as out_file:
    output.write(out_file)

however, the file is output as "{author_id}-pdf01.pdf"


Solution

  • Use:

    with open("media_cdn/account/{0}-pdf01.pdf".format(author_id), 'wb') as out_file:
    

    in place of

    with open('media_cdn/account/{author_id}-pdf01.pdf', 'wb') as out_file:
    

    'media_cdn/account/{author_id}-pdf01.pdf' is a string. {author_id} part of this string will NOT be substituted by author_id variable by itself.