Django:将 pdf 从视图保存到模型(reportlab)
Django: save pdf from view to model (reportlab)
是否可以在 Django 中将 pdf 从 view 保存到 型号(同时下载)?
到目前为止,这些步骤已经奏效:
- pdf显示在新标签中,我可以下载
- 模型实例已创建(但为空)
什么dows不工作:
- 创建的模型实例没有文件路径,服务器上也没有保存任何 PDF 文件
我的代码:
型号
from django.db import models
class TestModel(models.Model):
def pdf_upload_path(instance, filename):
return f'utility_bills/{instance.created_date.strftime("%Y-%m-%d")}_test_{filename}'
created_date = models.DateTimeField(
auto_now=False,
auto_now_add=True,
null=True,
blank=True,
)
pdf = models.FileField(upload_to=pdf_upload_path, blank=True)
观看次数
import io
from django.core.files.base import ContentFile
from django.http import FileResponse
from reportlab.platypus import SimpleDocTemplate, Paragraph
from .models import (TestModel)
## Create PDF ##
def utility_pdf(request):
# General Setup
pdf_buffer = io.BytesIO()
my_doc = SimpleDocTemplate(pdf_buffer)
sample_style_sheet = getSampleStyleSheet()
# Instantiate flowables
flowables = []
test = Paragraph("Test", sample_style_sheet['BodyText'])
# Append flowables and build doc
flowables.append(test)
my_doc.build(flowables)
# create and save pdf
pdf_buffer.seek(0)
pdf = pdf_buffer.getvalue()
file_data = ContentFile(pdf)
pdf = TestModel(pdf=file_data)
pdf.save()
response = FileResponse(pdf_buffer, filename="some_file.pdf")
return response
模板 *
可能值得一提的是,我不是从表单中获取 pdf 数据,而是从上一页的会话中获取数据:
{% extends 'core/base.html' %}
<!-- Title -->
{% block title %} Test Statement {% endblock %}
<!-- Body -->
{% block content %}
<div class="container test-submit-last">
<form>
<div class="col">
<a class="btn btn-primary" href="{% url 'test_pdf'%}" target="_blank" role="button">Show PDF</a>
</div>
</div>
</form>
</div>
{% endblock %}
我找到了!
我所要做的就是给我的 pdf 命名 - 现在它可以工作了!
pdf_data = pdf_buffer.getvalue()
file_data = ContentFile(pdf_data)
## Here is the crucial part that was missing: ##
file_data.name = 'test.pdf'
pdf = UtilityBill(lease=pdf_lease, pdf=file_data)
pdf.save()
response = FileResponse(pdf_buffer, filename="some_file.pdf")
是否可以在 Django 中将 pdf 从 view 保存到 型号(同时下载)?
到目前为止,这些步骤已经奏效:
- pdf显示在新标签中,我可以下载
- 模型实例已创建(但为空)
什么dows不工作:
- 创建的模型实例没有文件路径,服务器上也没有保存任何 PDF 文件
我的代码:
型号
from django.db import models
class TestModel(models.Model):
def pdf_upload_path(instance, filename):
return f'utility_bills/{instance.created_date.strftime("%Y-%m-%d")}_test_{filename}'
created_date = models.DateTimeField(
auto_now=False,
auto_now_add=True,
null=True,
blank=True,
)
pdf = models.FileField(upload_to=pdf_upload_path, blank=True)
观看次数
import io
from django.core.files.base import ContentFile
from django.http import FileResponse
from reportlab.platypus import SimpleDocTemplate, Paragraph
from .models import (TestModel)
## Create PDF ##
def utility_pdf(request):
# General Setup
pdf_buffer = io.BytesIO()
my_doc = SimpleDocTemplate(pdf_buffer)
sample_style_sheet = getSampleStyleSheet()
# Instantiate flowables
flowables = []
test = Paragraph("Test", sample_style_sheet['BodyText'])
# Append flowables and build doc
flowables.append(test)
my_doc.build(flowables)
# create and save pdf
pdf_buffer.seek(0)
pdf = pdf_buffer.getvalue()
file_data = ContentFile(pdf)
pdf = TestModel(pdf=file_data)
pdf.save()
response = FileResponse(pdf_buffer, filename="some_file.pdf")
return response
模板 *
可能值得一提的是,我不是从表单中获取 pdf 数据,而是从上一页的会话中获取数据:
{% extends 'core/base.html' %}
<!-- Title -->
{% block title %} Test Statement {% endblock %}
<!-- Body -->
{% block content %}
<div class="container test-submit-last">
<form>
<div class="col">
<a class="btn btn-primary" href="{% url 'test_pdf'%}" target="_blank" role="button">Show PDF</a>
</div>
</div>
</form>
</div>
{% endblock %}
我找到了!
我所要做的就是给我的 pdf 命名 - 现在它可以工作了!
pdf_data = pdf_buffer.getvalue()
file_data = ContentFile(pdf_data)
## Here is the crucial part that was missing: ##
file_data.name = 'test.pdf'
pdf = UtilityBill(lease=pdf_lease, pdf=file_data)
pdf.save()
response = FileResponse(pdf_buffer, filename="some_file.pdf")