Pierce O'Neill
Pierce O'Neill

Reputation: 383

Display purchase history from Django Admin

enter image description hereI am trying to show the purchase history per user on the each users dashboard. I have made some dummy orders in Stripe which are working but I am having difficulty displaying the orders in the order history section of the page.

I am also not sure the order is linking correctly to the registered user in the first place as I seem to be confusing myself with this method

Can somebody help me or point me in the right direction please?

<div class="container">
    <div class="row">
        <div class="col-sm-12 col-md-12 col-lg-12">
        	
    <table class="table table-striped">
    <h3>Your order history</h3>
    <hr>
    
  <tr>
    <th>Date</th>
    <th>Invoice ID</th> 
    <th>Description</th>
    <th>Total</th>
  </tr>
  {% for orders in orders %}
  <tr>
    <td scope="row">{{ order.quantity }}</td>
    <td>xxx</td>
    <td>xxx</td>
    <td>xxx</td>
  </tr>
  {% endfor %}

</table>
  
</div>

My checkout views.py

from django.contrib import messages, auth
from django.contrib.auth.decorators import login_required
from checkout.forms import MakePaymentForm
from django.shortcuts import render, get_object_or_404, redirect, reverse
from django.template.context_processors import csrf
from django.conf import settings
from babysitters.models import Babysitter
import stripe

# stripe.api_key = settings.STRIPE_SECRET


@login_required(login_url="/accounts/login")
def buy_now(request, id):
    if request.method == 'POST':
        form = MakePaymentForm(request.POST)
        if form.is_valid():
            try:
                babysitter = get_object_or_404(Babysitter, pk=id)
                customer = stripe.Charge.create(
                    amount= int(babysitter.price * 100),
                    currency="EUR",
                    description=babysitter.firstName,
                    card=form.cleaned_data['stripe_id'],
                )
            except (stripe.error.CardError):
                messages.error(request, "Your card was declined!")

            if customer.paid:
                messages.success(request, "You have successfully paid")
                return redirect(reverse('babysitters'))
            else:
                messages.error(request, "Unable to take payment")
        else:
            messages.error(request, "We were unable to take a payment with that card!")

    else:
        form = MakePaymentForm()
        babysitter = get_object_or_404(Babysitter, pk=id)

    args = {'form': form, 'publishable': settings.STRIPE_PUBLISHABLE, 'babysitter': babysitter}
    args.update(csrf(request))

    return render(request, 'checkout.html', args)

My Models.py file

from django.db import models
from babysitters.models import Babysitter
from django.contrib.auth.models import User

# Create your models here.

class Order(models.Model):
    first_name = models.CharField(max_length=255, blank=True)
    last_name = models.CharField(max_length=255, blank=True)
    address1 = models.CharField(max_length=255, blank=True)
    address2 = models.CharField(max_length=255, blank=True)
    city = models.CharField(max_length=20, null=True)
    county = models.CharField(max_length=100, null=True)
    postcode = models.CharField(max_length=7, null=True)
    email = models.CharField(max_length=50, blank=True, null=True)
    phone = models.CharField(max_length=10, blank=True)
    date = models.DateField()
    
    def __str__(self):
        return "{0}-{1}-{2}".format(self.id, self.date, self.user)
 
        
        
class OrderLineItem(models.Model):
    user = models.ForeignKey(User, null=False),
    order = models.ForeignKey(Order, null=False, related_name='orders')
    babysitter = models.ForeignKey(Babysitter, null=False)
    quantity = models.IntegerField(blank=False)
    
    def __str__(self):
        return "{0} {1} @ {2}".format(self.quantity, self.babysitter.firstName, self.babysitter.price)

Upvotes: 1

Views: 1975

Answers (1)

Will Keeling
Will Keeling

Reputation: 23024

The order history isn't showing because you're using a loop variable named orders but you're referring to order when accessing each attribute. Rename the loop variable to order:

{% for order in orders %}
<tr>
    <td scope="row">{{ order.quantity }}</td>
    <td>xxx</td>
    <td>xxx</td>
    <td>xxx</td>
</tr>
{% endfor %}

Upvotes: 1

Related Questions