gpt4 book ai didi

python - Django - 使用 xhtml2pdf 将生成的 PDF 保存在模型中

转载 作者:行者123 更新时间:2023-12-02 19:16:49 28 4
gpt4 key购买 nike

我正在尝试使用 xhtml2pdf 应用程序保存生成的 PDF。我希望它将 pdf 保存到模型中。 pdf 已完美创建,浏览器会下载该文件,但我不想下载,而是将其保存到模型中,在我的 models.py 中,我告诉它保存到媒体文件夹中的文件夹中。

模型.py:

class purchase(models.Model):
purchase_id = models.AutoField(primary_key=True)
transaction_date = models.DateTimeField()
method_payment = models.CharField(max_length = 20)
cmr_name = models.CharField(max_length = 60)
cmr_address = models.CharField(max_length = 90)
cmr_postcode = models.CharField(max_length = 10)
cmr_tel = models.CharField(max_length = 14)
cmr_email = models.CharField(max_length = 40)
cmr_pod = models.FileField(upload_to='customers_id')
cmr_signature = JSignatureField()
receipt = models.FileField(upload_to='receipts', null=True, blank=True)
added_by = models.CharField(max_length = 30)
item_brand = models.CharField(max_length = 50)
product = models.CharField(max_length = 100)
model = models.CharField(max_length = 100)
serial = models.CharField(max_length = 50)
item_brand2 = models.CharField(max_length = 50)
product2 = models.CharField(max_length = 100)
model2 = models.CharField(max_length = 100)
serial2 = models.CharField(max_length = 50)
item_brand3 = models.CharField(max_length = 50)
product3 = models.CharField(max_length = 100)
model3 = models.CharField(max_length = 100)
serial3 = models.CharField(max_length = 50)
def __str__(self):
return '{}'.format(self.cmr_name)

Utils.py:

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template

from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None

views.py:

@login_required(login_url='/accounts/login/')
def add_purchase(request):
username = None
if request.user.is_authenticated:
username = request.user.username
viewing_info = pwl.objects.get(pk='1')
viewing_ot = opening_times.objects.get(pk='1')
viewing_brands = watch_brands.objects.all()
viewing_watches = watch.objects.values_list('watch_brand_name', flat=True).distinct()
template = loader.get_template('pwl-access/add_purchase.html')
if request.method == 'POST':
form = add_purchase_form(request.POST, request.FILES)
if form.is_valid():
## Form Data
transaction_date = str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
added_by = request.user
cmr_name = form.cleaned_data['cmr_name']
cmr_address = form.cleaned_data['cmr_address']
cmr_postcode = form.cleaned_data['cmr_postcode']
cmr_tel = form.cleaned_data['cmr_tel']
cmr_email = form.cleaned_data['cmr_email']
purchase_price = form.cleaned_data['purchase_price']
method_payment = form.cleaned_data['method_payment']
item_brand = form.cleaned_data['item_brand']
product = form.cleaned_data['product']
model = form.cleaned_data['model']
serial = form.cleaned_data['serial']
price = form.cleaned_data['price']
item_brand2 = form.cleaned_data['item_brand2']
product2 = form.cleaned_data['product2']
model2 = form.cleaned_data['model2']
serial2 = form.cleaned_data['serial2']
price2 = form.cleaned_data['price2']
item_brand3 = form.cleaned_data['item_brand3']
product3 = form.cleaned_data['product3']
model3 = form.cleaned_data['model3']
serial3 = form.cleaned_data['serial3']
price3 = form.cleaned_data['price3']
cmr_pod = request.FILES['cmr_pod'] if 'cmr_pod' in request.FILES else False
cmr_signature = form.cleaned_data.get('cmr_signature')
receipt = False
if cmr_signature:
signature_picture = draw_signature(cmr_signature, as_file=True)
new_purchase = purchase(transaction_date=transaction_date,
added_by=added_by,
cmr_name=cmr_name,
cmr_address=cmr_address,
cmr_postcode=cmr_postcode,
cmr_tel=cmr_tel,
cmr_email=cmr_email,
cmr_pod=cmr_pod,
cmr_signature=signature_picture,
receipt=receipt,
method_payment=method_payment,
item_brand=item_brand,
product=product,
model=model,
serial=serial,
item_brand2=item_brand2,
product2=product2,
model2=model2,
serial2=serial2,
item_brand3=item_brand3,
product3=product3,
model3=model3,
serial3=serial3, )
new_purchase.save()

##Receipt Creation
cb_data = {
'purchase_price': purchase_price,
'price': price,
'price2': price2,
'price3': price3,
'new_purchase': new_purchase,
'cmr_pod': cmr_pod,
'signature_picture': signature_picture,
}
template = get_template('pwl-access/email-templates/receipt_p_email.html')
pdf = render_to_pdf('pwl-access/email-templates/receipt_p_pdf.html', cb_data)
purch_id = new_purchase.pk
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = 'purchase_%s.pdf' % (purch_id)
download = request.GET.get('download')
content = "attachment; filename=%s " % (filename)
response['Content-Disposition'] = content
receipt_file = File(BytesIO(pdf.content))
purchase.objects.filter(pk=purch_id).update(receipt=receipt_file)

email_subject = 'Your Receipt from - ' + transaction_date
email_to = 'email'
filled_email = template.render(cb_data)
msg = EmailMultiAlternatives(email_subject, filled_email, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89ece7f8fce0fbe0ecfac9a7eae6a7fce2" rel="noreferrer noopener nofollow">[email protected]</a>',
[email_to], )
msg.content_subtype = "html"

if email_subject:
try:
msg.send()
except BadHeaderError:
return HttpResponse('Invalid header found.')
messages.success(request, 'Success! Purchase added and email has been sent to the customer ')
return response
else:
form = add_purchase_form()

context = {
'viewing_info': viewing_info,
'viewing_ot': viewing_ot,
'viewing_brands': viewing_brands,
'viewing_watches': viewing_watches,
'form': form,
}
return HttpResponse(template.render(context, request))

管理和数据库显示没有上传任何内容,我没有收到任何错误。在views.py文件中,这些代码行是我希望用pdf文件更新数据库的代码:

receipt_file = File(BytesIO(pdf.content))
purchase.objects.filter(pk=purch_id).update(receipt=receipt_file)

不幸的是,它没有做任何事情。有人可以帮忙吗?被困在这个有一段时间了。我已经尝试了在 stackoverflow 和其他网站上看到的其他几个答案..

谢谢,如有任何帮助,我们将不胜感激。

编辑1:我尝试调用 save() 而不是 update,但没有什么区别:

                receipt_file = File(BytesIO(pdf.content))
purch = purchase.objects.get(pk=purch_id)
purch.receipt = receipt_file
purch.save()

Edit2:我也尝试了多个版本的内容和 IOString(content) 没有任何效果

Edit3:仍然没有答案,任何 Django 专家可能知道发生了什么事吗?看起来好像很简单的事情..

最佳答案

最后我设法使用以下代码来做到这一点:

                receipt_file = BytesIO(pdf.content)
purch_upd = purchase.objects.get(pk=purch_id)
purch_upd.receipt = File(receipt_file, filename)
purch_upd.save()

如此简单,不敢相信我在这个问题上坚持了这么久。

关于python - Django - 使用 xhtml2pdf 将生成的 PDF 保存在模型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63598055/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com