我正在尝试在每个用户的信息中心上显示每个用户的购买历史记录。我已经在Stripe中创建了一些虚拟订单,这些订单正在运行,但是在页面的“订单历史记录”部分中显示订单时遇到困难。
我也不确定该订单最初是否正确链接到注册用户,因为我似乎对此方法感到困惑
请有人帮我或指出正确的方向吗?
<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>
我的结帐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)
我的Models.py文件
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)
答案 0 :(得分:1)
未显示订单历史记录,因为您正在使用名为orders
的循环变量,但是在访问每个属性时却引用了order
。将循环变量重命名为order
:
{% for order in orders %}
<tr>
<td scope="row">{{ order.quantity }}</td>
<td>xxx</td>
<td>xxx</td>
<td>xxx</td>
</tr>
{% endfor %}