【问题标题】:Django render to pdf with buttonDjango 使用按钮渲染为 pdf
【发布时间】:2019-03-20 12:09:23
【问题描述】:

我有def,它通过对 django-admin 的操作呈现为 pdf。

def Print(self, request, obj):
    data = {
            'obj':obj
    }
    pdf = render_to_pdf('daa/imprimir/pdf.html', data)
    if pdf :
        response = HttpResponse(pdf, content_type='application/pdf')
        filename ="Avaria_%s.pdf" %("123451231")
        content = "inline; filename='%s'" %(filename)
        response['Content-Disposition'] = content
        download = request.GET.get("download")
        if download:
                content = "attachment; filename='%s'" %(filename)
        response['Content-Disposition'] = content
        return  response
    return HttpResponse("Not found")

关于我的行为,我有:

class ModelAdmin(admin.ModelAdmin):
    actions = [Print]

而且一切正常,我选择了我想要渲染的对象,在我的html 中,我有一个 cicle,列出了我想要的那些 obj 的所有字段。

但现在我不想将列表呈现为 pdf。我只想渲染 1 个 obj。所以我创建了一个自定义按钮来做到这一点。

http://prntscr.com/muijhl

所以当我点击按钮时,我想将打开的 obj 渲染为 pdf。我不知道我需要做什么来获取我的定义,但在按钮内部

【问题讨论】:

    标签: django pdf django-admin rendering


    【解决方案1】:

    关于如何将此代码作为视图与它自己的 url there's a perfect example in the official doc 挂钩(但您必须知道要查找什么才能找到它)

    然后您必须override your change_form template 添加指向此网址的按钮/链接。

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 2019-02-13
      • 2014-03-15
      相关资源
      最近更新 更多