gpt4 book ai didi

python - 如何在 Django 分页中加载 Stripe 对象

转载 作者:行者123 更新时间:2023-12-01 07:29:45 25 4
gpt4 key购买 nike

我想对 Stripe 的发票进行分页

@login_required
def invoice_list(request):
customer = stripe.Customer.list(email=request.user.username)
for invoice_search in customer['data']:
customer_invoice_list = stripe.Invoice.list(customer=invoice_search['id'])
page = request.GET.get("page", 1)
paginator = Paginator(customer_invoice_list, 20)

try:
invoices = paginator.page(page)
except PageNotAnInteger:
invoices = paginator.page(1)
except EmptyPage:
invoices = paginator.page(paginator.num_pages)

context = {
'invoices': invoices,
}
return render(request, 'invoice_list.html', context)

这是我的代码,我收到错误:Unhashable 类型

在Django 1.11文档中说可以加载列表或查询集,我获取发票列表,为什么不能分页?

最佳答案

在创建分页对象之前尝试将其转换为元组。请注意,我还没有亲自测试过这一点 - 可能您遇到了与此人相同的问题 - Pagination doesn't accept dict as data - unhashable type

    @login_required
def invoice_list(request):
customer = stripe.Customer.list(email=request.user.username)
for invoice_search in customer['data']:
customer_invoice_list = stripe.Invoice.list(customer=invoice_search['id'])
page = request.GET.get("page", 1)
customer_invoice_list = tuple(customer_invoice_list)
page = tuple(page)
paginator = Paginator(customer_invoice_list, 20)

try:
invoices = paginator.page(page)
except PageNotAnInteger:
invoices = paginator.page(1)
except EmptyPage:
invoices = paginator.page(paginator.num_pages)

context = {
'invoices': invoices,
}
return render(request, 'invoice_list.html', context)

关于python - 如何在 Django 分页中加载 Stripe 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57263583/

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