将id参数传递到Djang中导入的类

2024-06-16 09:35:20 发布

您现在位置:Python中文网/ 问答频道 /正文

在Django中,我有一个基于函数的视图,负责在pdf文件中打印所有注册用户的详细信息(实际上只是名称)

def test_pdf(request, id):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="My Users.pdf"'

    buffer = io.BytesIO()

    report = MyPrint(buffer, 'Letter', id)
    pdf = report.print_users()

    response.write(pdf)
    return response

此函数之所以有效,是因为我在views.py文件中导入了另一个文件中构建的类,该类负责绘制pdf,MyPrint:

from reportlab.lib.pagesizes import letter, A4
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_CENTER
from django.contrib.auth.models import User


class MyPrint:
    def __init__(self, buffer, pagesize):
        self.buffer = buffer
        if pagesize == 'A4':
            self.pagesize = A4
        elif pagesize == 'Letter':
            self.pagesize = letter
            self.width, self.height = self.pagesize
    def print_users(self):
        buffer = self.buffer
        doc = SimpleDocTemplate(buffer,
        rightMargin=72,
        leftMargin=72,
        topMargin=72,
        bottomMargin=72,
        pagesize=self.pagesize)
        # Our container for 'Flowable' objects
        elements = []

        # A large collection of style sheets pre-made for us
        styles = getSampleStyleSheet()
        styles.add(ParagraphStyle(name='centered', alignment=TA_CENTER))

        # Draw things on the PDF. Here's where the PDF generation happens.
        # See the ReportLab documentation for the full list of functionality.
        users = User.objects.all()
        elements.append(Paragraph('My User Names', styles['Heading1']))
        for i, user in enumerate(users):
            elements.append(Paragraph(user.get_full_name(), styles['Normal']))

        doc.build(elements)

        # Get the value of the BytesIO buffer and write it to the response.
        pdf = buffer.getvalue()
        buffer.close()
        return pdf

现在,如果我将相对pk传递到函数中,如何使函数和类特定于用户?除了更新urlpattern之外,我应该将id传递到类和/或函数中吗


Tags: 文件the函数fromimportselfforpdf
1条回答
网友
1楼 · 发布于 2024-06-16 09:35:20

如果您想让现有函数与一个或多个用户一起工作,并且如果不传入id则继续工作,我认为最简单的更改方法如下:

def print_users(self, id=None):
        buffer = self.buffer
        doc = SimpleDocTemplate(buffer,
        rightMargin=72,
        leftMargin=72,
        topMargin=72,
        bottomMargin=72,
        pagesize=self.pagesize)
        # Our container for 'Flowable' objects
        elements = []

        # A large collection of style sheets pre-made for us
        styles = getSampleStyleSheet()
        styles.add(ParagraphStyle(name='centered', alignment=TA_CENTER))

        # Draw things on the PDF. Here's where the PDF generation happens.
        # See the ReportLab documentation for the full list of functionality.
        users = User.objects.all()
        if id:
            users = users.filter(id__in=id)
        elements.append(Paragraph('My User Names', styles['Heading1']))
        for i, user in enumerate(users):
            elements.append(Paragraph(user.get_full_name(), styles['Normal']))

        doc.build(elements)

        # Get the value of the BytesIO buffer and write it to the response.
        pdf = buffer.getvalue()
        buffer.close()
        return pdf

然后将其命名方式更改为:

report = MyPrint(buffer, 'Letter')
pdf = report.print_users(id)

或者,如果要打印所有用户,只需将其命名为:

report = MyPrint(buffer, 'Letter')
pdf = report.print_users()

相关问题 更多 >