0

I am creating and saving a PDF as such in my views:

views.py

@login_required(login_url="/login")
def PackingListView(request):
    if request.method == "POST":
        form = PackingListForm(request.POST)
        if form.is_valid():
            if 'preview' in request.POST:
                ...

            elif 'save' in request.POST:
                pdf_contents = form
                file = ContentFile(pdf_contents)

                item = PackingListDocuments.objects.get(pk=1)

                item.PackingListDocument.save('test.pdf', file) #saving as FileField in model


                form.save()
                messages.success(request, "Success: Packing List Has Been Created!")
                return redirect('HomeView')

I see that the test.pdf is saved. I can see it in my file explorer as well as in the admin, but every time that I attempt to open it, the file seems to be corrupted. What do I need to add or subtract in my code to get this working?

Thanks!

UPDATE:

I've changed the line: file = ContentFile(pdf_contents) to file = File(pdf_contents)

But now I am receiving an attribute error that 'PackingListForm' object has no attribute 'read'

GXM100
  • 417
  • 6
  • 20

1 Answers1

0

I believe the error must be to do with this line

file = ContentFile(pdf_contents)

Note that, from the docs

The ContentFile class inherits from File, but unlike File it operates on string content (bytes also supported), rather than an actual file. For example:

So my guess is that you are not passing in a string/byte type as argument to the ContenetFile object.

Try finding the type of it. You can also convert it to string type by doing String(pdf_contents).

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • Thanks for the reply - ok so I change ```ContentFile``` to ```File``` but no I am getting an error "Attribute Error: 'PackingListForm' object has no attribute 'read'" Any thoughts on that? – GXM100 Mar 17 '20 at 14:14
  • The file object that you using is it python's built-in object or Django's instance of django.core.files.File? – AzyCrw4282 Mar 17 '20 at 14:22
  • it is django.core.files.File – GXM100 Mar 17 '20 at 23:49
  • I can only conclude then by saying you are not passing a correct type of data as the argument to File instance. You might wanna print `pdf_contents` and see what it actually is. – AzyCrw4282 Mar 17 '20 at 23:59
  • hmm interesting - it seems to print the .html template which is used in that form. Is there a different way in which I should pass the content of the form to the File instace? – GXM100 Mar 18 '20 at 00:12
  • I am afraid i cant think why its going wrong. You may try other methods, see [here](https://www.codingforentrepreneurs.com/blog/html-template-to-pdf-in-django) for example – AzyCrw4282 Mar 18 '20 at 00:57
  • Yes I looked at this but this does not show how to save to a FileField only how to download it, which is great. But I couldn’t figure out how to get from there to saving the pdf in my model – GXM100 Mar 18 '20 at 01:17
  • I think i found it. You need to do the following before saving to your models ` html = template.render(context) result = StringIO.StringIO()`. See here https://stackoverflow.com/q/52176999/6505847 – AzyCrw4282 Mar 18 '20 at 01:19