【问题标题】:Django admin action not repeatingDjango 管理操作不重复
【发布时间】:2021-03-27 15:44:33
【问题描述】:

我有以下管理操作(在我的 admin.py 文件中),旨在下载选定项目的 pdf 文件。除了它只会为查询集中的第一个项目创建和下载 pdf 之外,它似乎还在工作。我认为问题在于“返回响应”行,但我不知道还有什么可以代替它的。任何输入都会很棒,我被踩到了!

@admin.register(ReleaseForm)
class ReleaseAdmin(admin.ModelAdmin):
    def participant(self, obj):
        return str(obj.customer.last_name) + ", " + str(obj.customer.first_name)
    def training(self, obj):
        return str(obj.order.training_registered.name)

    def print_release(self, request, queryset):
        updated=queryset.count()
        print (updated)
        for obj in queryset.all():
            customer=obj.customer
            order=Order.objects.get(customer=customer)
            firstname = obj.customer.first_name
            lastname = obj.customer.last_name
            nwta = order.training_registered.name

            data = {'order':order,'firstname': firstname, 'lastname': lastname, 'nwta':nwta,}
            pdf=release_render_to_pdf('accounts/pdf_template.html', data)
            response = HttpResponse(pdf, content_type='application/pdf')
            filename = "Release_%s_%s.pdf" %(lastname,nwta,)
            content="attachment; filename=%s" %(filename)
            response['Content-Disposition'] = content
            print(obj.customer)
            return response
        
            self.message_user(request, ngettext(
            '%d Relase Form was successfully printed.',
            '%d Relase Forms were successfully printed.',
            updated,
            ) % updated, messages.SUCCESS)
    print_release.short_description="Print Release Form(s)"

    list_display = ('participant','release_status','release_date_submitted' ,'note' )
    actions = ['print_release']
    ordering = ('customer',)
    list_filter = ('customer__order__training_registered__training__name','customer__order__training_registered', 'customer__order__regstatus','release_status','release_date_submitted' ,'note')
    search_fields = ('participant','note')

【问题讨论】:

    标签: django action admin xhtml2pdf


    【解决方案1】:

    返回后,循环(和print_release 方法)已退出。这显然是一个错误。您希望在函数结束时返回,而不是中途返回。

    我不会为你写你的程序,但显然你需要累积部分结果,而不是在循环中间返回,然后在最后返回累积的结果。

    【讨论】:

      猜你喜欢
      • 2021-08-03
      • 2011-04-28
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多