Ни один заказ не соответствует заданному запросу
Я изучаю 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 patterns from urls.py of Project:
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
, не существует в базе данных, или значение пустое.