python – 使用Django报告标头的参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 使用Django报告标头的参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1513字,纯文字阅读大概需要3分钟。
内容图文
![python – 使用Django报告标头的参数](/upload/InfoBanner/zyjiaocheng/725/9cd65a86d42e45348362739e5c46c302.jpg)
我正在根据this example生成PDF并且工作正常,但是我在标题部分有一点问题,这是实际的代码:
def _header_footer(canvas, doc):
# Save the state of our canvas so we can draw on it
canvas.saveState()
styles = getSampleStyleSheet()
# Header
header = Paragraph('This is a multi-line header. It goes on every page. ' * 5, styles['Normal'])
w, h = header.wrap(doc.width, doc.topMargin)
header.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin - h)
# Release the canvas
canvas.restoreState()
我想将数据从模型发送到标题,就像这样
def _header_footer(canvas, doc, custom_data):
canvas.saveState()
styles = getSampleStyleSheet()
header = Paragraph('This is my %s' % custom_data')
#etc.
_header_footer被调用:
doc.build(elements, onFirstPage=self._header_footer, onLaterPages=self._header_footer)
如何将custom_data变量发送到_header_footer方法?
解决方法:
这里至少有两个选项:
您可以使用partial from functools,它允许将某些参数“绑定”到您的函数中.例如:
from functools import partial
def _header_footer(canvas, doc, custom_data):
...
# Usage:
doc.build(elements, onFirstPage=partial(_header_footer, custom_data=my_custom_data))
或者因为你似乎在一个类中使用它,由于self关键字(或者它是一个错字?),你可以简单地将custom_data作为你的类的属性.
class MyPdf(object):
def __init__(self, custom_data):
self.custom_data = custom_data
self.doc = ... # your doc
def _header_footer(self, canvas, doc):
# you can access self.custom_data here
...
def build(self):
...
self.doc.build(elements, onFirstPage=self._header_footer)
# Usage
my_pdf = MyPdf(custom_data)
my_pdf.build()
内容总结
以上是互联网集市为您收集整理的python – 使用Django报告标头的参数全部内容,希望文章能够帮你解决python – 使用Django报告标头的参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。