Jak zapisać plik PDF na dysku w folderze Media zamiast zwracać go jako odpowiedź w przeglądarce

1

Ten kod zwraca plik .pdf jako odpowiedź w przeglądarce. A jak zapisać plik w folderze media na serwerze? powie mi ktoś?

# Create a file-like buffer to receive PDF data.
buffer = io.BytesIO()

# Create the PDF object, using the buffer as its "file."
p = canvas.Canvas(buffer)

# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of functionality.
p.drawString(30, 800, 'invoice num: ' + request.data['invoice_num'])
p.drawString(30, 750, 'issuer: ' + request.data['issuer'])
p.drawString(30, 700, 'header: ' + request.data['header'])
p.drawString(30, 650, 'notes: ' + request.data['notes'])

# Close the PDF object cleanly, and we're done.
p.showPage()
p.save()

# FileResponse sets the Content-Disposition header so that browsers
# present the option to save the file.
buffer.seek(0)

return FileResponse(buffer, as_attachment=True, filename=serializer.data['invoice_num'] + '.pdf')
49

Obczaj

from django.core.files.storage import default_storage
1

ok juz sobie poradzilem

1 użytkowników online, w tym zalogowanych: 0, gości: 1