我正在关注 django ecommerce 的教程,在尝试测试 Stripe 应用程序时遇到了 404 错误。具体来说,我在提交测试付款后被重定向到“ http://127.0.0.1:8000/your-charge-code ”。
在views.py中
def checkout(request):
publishKey = settings.STRIPE_PUBLISHABLE_KEY
if request.method == 'POST':
token = request.POST['stripeToken']
try:
charge = stripe.Charge.create(
amount=1000,
currency="gbp",
description="Example charge",
source=token,
)
except stripe.error.CardError as e:
pass
context ={'publishKey': publishKey}
template = 'checkout.html'
return render(request,template,context)
在 checkout.html 中:
{% block strip %}
<script type="text/javascript">
Stripe.setPublishableKey('{{ publishKey }}');
function stripeResponseHandler(status, response) {
// Grab the form:
var $form = $('#payment-form');
if (response.error) { // Problem!
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false); // Re-enable submission
} else { // Token was created!
// Get the token ID:
var token = response.id;
// Insert the token into the form so it gets submitted to the server:
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// Submit the form:
$form.get(0).submit();
}
}
</script>
{% endblock %}
{% block jquery %}
$(function() {
var $form = $('#payment-form');
$form.submit(function(event) {
$form.find('.submit').prop('disabled',true);
Stripe.card.createToken($form,stripeResponseHandler);
return false;
});
});
{% endblock %}
在 base.html 中
{% block script %}
{% endblock %}
<script>
$(document).ready(function(){
{% block jquery %}
{% endblock %}
});
</script>
我究竟做错了什么?为什么“提交”按钮会自动将我重定向到 ./your-charge-code/ 网址?我是 django 和 web 开发的新手,希望这里的一些大师可以指导我。谢谢!