0

I'm working with STRIPE Gateway Payment on Django and I'm facing problems for access to dictionary on templates.

I already do on pure python and work fine.

This is my view

@login_required
def invoice_details(request):
    customer = stripe.Customer.list(email=request.user)
    return render(request, 'payment.html', customer)

and in template this is my code:

<h2>{% trans "User details" %}</h2>
{% for actual_customer in customer.data %}
  ID: {{ actual_customer.id }}
{% endfor %}

The above code isn't working, any help is appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

Usually it's helpful to just output the whole context variable to see if any data is there and what keys are available to you.

<h2>{% trans "User details" %}</h2>
{{ customer }}
{% for actual_customer in customer.data %}
    ID: {{ actual_customer.id }}
{% endfor %}

Rendering your template with above would show you that {{ customer }} doesn't return anything. That's because customer is your variable name that you're passing as the context. If you wanted customer to be a key you would have to modify your view like below

@login_required
def invoice_details(request):
    context = dict()
    context['customer'] = stripe.Customer.list(email=request.user)
    return render(request, 'payment.html', context)
bromosapien
  • 234
  • 1
  • 5
  • I do what you say and the output isn't good, only can get principal tree in this dict: User details { "data": [], "has_more": false, "object": "list", "url": "/v1/customers" } – Aluisco Ricardo Mastrapa Jul 24 '19 at 02:41
0

The problem was in view function, because I loaded user instance and not the username, that's how can do and work perfect:

@login_required
def invoice_details(request):
    customer = stripe.Customer.list(email=request.user.username)
    context = {
        'customer': customer,
    }
    return render(request, 'payment.html', context)