Я разрабатываю сайт электронной коммерции на django. При интеграции оплаты в проект и запуске сервера я получил ошибку page not found (404)
Ошибка, которую я получил, когда ввел данные клиента и нажал на checkout:
Вот мой views.py для оплаты. Здесь я импортировал модуль stripe, используя stripe api, и создал сессию checkout:
from django.shortcuts import render, redirect, reverse, get_object_or_404
from decimal import Decimal
import stripe
from django.conf import settings
from orders.models import Order
# Create your views here.
stripe.api_key = settings.STRIPE_SECRET_KEY
stripe.api_version = settings.STRIPE_API_VERSION
def payment_process(request):
order_id = request.session.get('order_id', None)
order = get_object_or_404(Order, id=order_id)
if request.method == 'POST':
success_url = request.build_absolute_uri(reverse('payment:completed'))
cancel_url = request.build_absolute_uri(reverse('payment:canceled'))
session_data = {
'mode': 'payment',
'client_reference_id': order_id,
'success_url': success_url,
'cancel_url': cancel_url,
'line_items': []
}
for item in order.items.all():
session_data['line_items'].append({
'price_data': {
'unit_amount': int(item.price * Decimal(100)),
'currency': 'usd',
'product_data': {
'name': item.product.name,
},
},
'quantity': item.quantity,
})
session = stripe.checkout.Session.create(**session_data)
return redirect(session.url, code=303)
else:
return render(request, 'payment/process.html', locals())
def payment_completed(request):
return render(request, 'payment/completed.html')
def payment_canceled(request):
return render(request, 'payment/canceled.html')
Это urls.py для оплаты, где я создал шаблоны URL для процесса, где отображается сводка заказа, сессия stripe checkout. completed для успешного платежа и canceled для отмены платежа. :
from django.urls import path
from . import views
app_name = 'payment'
urlpatterns = [
path('canceled/', views.payment_canceled, name='canceled'),
path('completed/', views.payment_completed, name='completed'),
path('process/', views.payment_process, name='process'),
]
Это djangoProject urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
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('ecommerce.urls', namespace='ecommerce')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)