【问题标题】:Django - Stripe. You did not set a valid publishable key. Error that comes up when trying to incorprate stripe payments on a django websiteDjango - 条纹。您没有设置有效的可发布密钥。尝试在 django 网站上合并条带付款时出现的错误
【发布时间】:2020-08-02 17:08:13
【问题描述】:

我正在开发一个 django 书店网站,但条带集成似乎存在错误。我有一个要求支付信息的订单页面(我现在正在使用测试 API)。我收到同样的错误“您没有设置有效的可发布密钥。使用您的可发布密钥调用 Stripe.setPublishableKey()。”

orders/views.py

from django.conf import settings
from django.views.generic.base import TemplateView

class OrdersPageView(TemplateView):
    template_name = 'orders/purchase.html'

    def get_context_data(self, **kwargs):
        ##Stripe.setPublishableKey('PUBLISHABLE_KEY')
        context = super().get_context_data(**kwargs)
        context['stripe_key'] = settings.STRIPE_TEST_PUBLISHABLE_KEY
        return context

模板/订单/purchase.html

{% extends '_base.html' %}

{% block title %}Orders{% endblock title %}

{% block content %}
<h1>Orders page</h1>
<p>Buy for $39.00</p>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="{{ stripe_key }}"
    data-description="All Books"
    data-amount="3900"
    data-locale="auto">
</script>
{% endblock content %}

【问题讨论】:

    标签: python django stripe-payments


    【解决方案1】:

    在您的表单中,您需要一个 {% csrf_token %}form action 以将您带到收费页面。

    <form action="{% url 'charge' %}" method="post">
    {% csrf_token %}
    
    <h1>Orders page</h1>
    <p>Buy for $39.00</p>
    
    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="{{ stripe_key }}"
        data-description="All Books"
        data-amount="3900"
        data-locale="auto">
    </script>
    
    </form>
    

    此外,除非从未在模板中设置定价,否则这是自找麻烦。将它们设置在您的视图中。

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 2016-01-01
      • 2018-10-18
      • 2021-09-22
      • 2020-10-12
      • 2021-05-11
      • 2015-01-19
      • 1970-01-01
      相关资源
      最近更新 更多