我正在学习Django的书叫做Django 3的例子。我按照书中给出的步骤来做。但我得到以下错误:
Page not found (404)
No Order matches the given query.
Request Method: GET
Request URL: http://127.0.0.1:8000/payment/process/
Raised by: payment.views.payment_process
Using the URLconf defined in FlixKart.urls, Django tried these URL patterns, in this order:
admin/
cart/
orders/
payment/ process/ [name='process']
The current path, payment/process/, matched the last one.
views.py支付应用程序:
def payment_process(request):
order_id = request.session.get('order_id')
order = get_object_or_404(Order, id=order_id)
total_cost = order.get_total_cost()
if request.method == 'POST':
# retrieve nonce
nonce = request.POST.get('payment_method_nonce', None)
# create and submit transaction
result = gateway.transaction.sale({
'amount': f'{total_cost:.2f}',
'payment_method_nonce': nonce,
'options': {
'submit_for_settlement': True
}
})
if result.is_success:
# mark the order as paid
order.paid = True
# store the unique transaction id
order.braintree_id = result.transaction.id
order.save()
return redirect('payment:done')
else:
return redirect('payment:canceled')
else:
# generate token
client_token = gateway.client_token.generate()
return render(request, 'payment/process.html', {'order': order,'client_token': client_token})
url模式来自Project的urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('cart/', include('cart.urls', namespace='cart')),
path('orders/', include('orders.urls', namespace='orders')),
path('payment/', include('payment.urls', namespace='payment')),
path('', include('shop.urls', namespace='shop')),
]
我从stackoverflow尝试了一些解决方案,但没有一个适合我。请帮我解决这个错误。谢谢你。
页面正确解决,但No Order matches the given query
提示404错误是由以下线。
order = get_object_or_404(Order, id=order_id)
表示数据库中不存在ID匹配order_id
的记录,或者该值为空。