将django反向链接到PK <int:pk>的URL

问题描述

我正在按照REPL教程使用PayPal设置付款

我的问题是paypal_dict包含其中

@H_502_5@paypal_dict = {
...
'return_url': 'http://{}{}/{}'.format(host,reverse('payment_done')),...
}

,我的payment_done URL需要具有交易本身的ID,我不确定如何在其中包含一个PK 这是我的完整URLs.py

@H_502_5@...
# Payment
    path('payment/process/<int:Trade_id>/',payment_views.payment_process,name="payment_process"),path('payment/done/<int:Trade_id>/',payment_views.payment_done,name="payment_done"),...

我的完整process_paypal

@H_502_5@def payment_process(request,Trade_id):
    Trade = get_object_or_404(Trade,id=Trade_id)
    host = request.get_host()

    paypal_dict = {
        'business': settings.PAYPAL_RECEIVER_EMAIL,# todo WHO TO PAY
        'amount': Decimal(Trade.price),'item_name': Trade.filename,'invoice': str(Trade.id),'currency_code': 'USD','notify_url': 'http://{}{}'.format(host,reverse('paypal-ipn')),'return_url': 'http://{}{}/{}'.format(host,'cancel_return': 'http://{}{}'.format(host,reverse('home')),}

    form = PayPalPaymentsForm(initial=paypal_dict)
    return render(request,'payment/payment_process.html',{'Trade': Trade,'form': form})

解决方法

您可以使用trade_idargs在网址中传递kwargs

paypal_dict = {
...
'return_url': 'http://{}{}'.format(host,reverse('payment_done',kwargs={'trade_id': trade.id})),...
}

docs ref