首页 > 解决方案 > 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 %}

提前感谢您的任何支持。

标签: pythonpython-3.xdjangodjango-views

解决方案


您似乎没有为消息添加条件。将以下代码添加到 base.html(我会推荐)。

{% if messages %}
  {% for message in messages %}
    <div class="alert alert-{{ message.tags }}">{{ message }}</div>
  {% endfor %}
{% endif %}


推荐阅读