python - Django中未显示成功/错误消息?
问题描述
我不太清楚为什么,但是当我在我的网站上完成结帐过程时,没有显示成功/错误消息。例如,如果您成功付款,您应该会收到一条弹出消息“您已成功支付”,但不会弹出。
我不确定是否有人可以看到结帐视图的设置方式有任何问题?我在下面添加了它。
def checkout(request):
"""
Returns the checkout page and allows the
user to enter the personal and payment
details in order to complete their order
"""
if request.method == 'POST':
order_form = OrderForm(request.POST)
payment_form = MakePaymentForm(request.POST)
if order_form.is_valid() and payment_form.is_valid():
order = order_form.save(commit=False)
order.date = timezone.now()
customer = Customer.objects.get(user=request.user)
order.customer = customer
order.save()
cart = request.session.get('cart', {})
total = 0
for (id, quantity) in cart.items():
product = get_object_or_404(Product, pk=id)
total += quantity * product.price
order_line_item = OrderLineItem(order=order,
product=product,
quantity=quantity)
order_line_item.save()
try:
customer = stripe.Charge.create(amount=int(total
* 100), currency='GBP',
description=request.user.email,
card=payment_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')
request.session['cart'] = {}
return redirect(reverse('products'))
else:
messages.error(request, 'Unable to take payment')
else:
print(payment_form.errors)
messages.error(request,
'We were unable to take a payment with that card!'
)
else:
payment_form = MakePaymentForm()
order_form = OrderForm()
return render(request, 'checkout.html', {'order_form': order_form,
'payment_form': payment_form,
'publishable': settings.STRIPE_PUBLISHABLE})
此外,这里是结帐 HTML 模板。
{% block head_js %}
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
//<![CDATA[
Stripe.publishableKey = '{{ publishable }}'
//]]>
</script>
<script type="text/javascript" src="{% static 'js/stripe.js' %}"></script>
{% endblock head_js %}
{% block content %}
<div class="form-container base-wrapper">
<div class="form-styling justify-content-center">
<form role="form" method="post" id="payment-form" action="{% url 'checkout' %}">
{% csrf_token %}
<h4>Personal Details</h4>
<hr>
<div id="credit-card-errors" style="display:none">
<div class="alert-message block-message error" id="stripe-error-message"></div>
</div>
<div class="form-group">
{{ order_form | as_bootstrap }}
</div>
<h4>Card Details</h4>
<hr>
<div class="form-group">
{{ payment_form | as_bootstrap }}
</div>
<div class="form-group">
<input class=" btn btn-primary" id="submit_payment_btn" name="commit" type="submit" value="Submit Payment">
</div>
</form>
</div>
</div>
{% endblock %}
提前感谢您的任何支持。
解决方案
您似乎没有为消息添加条件。将以下代码添加到 base.html(我会推荐)。
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">{{ message }}</div>
{% endfor %}
{% endif %}
推荐阅读
- azure - Kubernetes 中的 Azure DevOps 构建代理
- javascript - Joi 嵌套时
- kubernetes - 为每个 statefulset pod 使用特定节点的正确方法是什么?
- node.js - 如何在 Angular 中使用 NodeJS/express/multer 访问存储为 mongodb 中的缓冲区的图像
- azure - 使用 ARM-TTK 测试 ARM 模板
- javascript - DataTable 更新到 1.11 销毁返回错误
- pandas - Groupby agg保持空白值
- github - 该应用程序需要以下 .net 框架版本之一。如何为github解决这个问题?
- java - 如何使用 io.reactivex.rxjava3.core
- php - Wordpress如何在转义html标签时使用换行符