尽管发送了一个参数,但没有找到任何参数的 'order_successful' 反转。姜戈

Reverse for 'order_successful' with no arguments not found despite sending an argument. DJANGO

我是 Django 的新手。我正在为客户制作网站。我正在集成一个 PayPal 客户端模块,并为此关注了 youtube 上的视频。 订单完成后,我尝试转到一个页面并将产品 ID 传递给它,以便它可以从数据库中检索它并显示一个漂亮的感谢页面。 但是我收到以下错误:

NoReverseMatch at /product-details/payment

**Reverse for 'order_successful' with no arguments not found. 1 pattern(s) tried: ['order_success/(?P<DATA>[^/]+)$']**

以下是我调用函数的页面 checkout.html:

<script>
          function getCookie(name) {
            let cookieValue = null;
            if (document.cookie && document.cookie !== '') {
              const cookies = document.cookie.split(';');
              for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i].trim();
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                  cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                  break;
                }
              }
            }
            return cookieValue;
          }
          const csrftoken = getCookie('csrftoken');

          var total = '{{price}}'
          var quantityBought = '{{quant}}'
          var prodId = '{{prod.id}}'
            
          var fName = '{{firstName}}'
          var lName = '{{lastName}}'
          var apt = '{{apt}}'
          var street = '{{street}}'
          var city = '{{city}}'
          var state = '{{state}}'
          var zipcode = '{{zipcode}}'
          var country = '{{country}}'
          var email = '{{email}}'
          var phone = '{{phone}}'

          async function completeOrder(){
            var url = "{% url 'paymentComplete' %}"
            const response = await fetch(url, {
              method: 'POST',
              headers:{
                  'Content-type': 'application/json',
                  'X-CSRFToken': csrftoken,
              },
              body:JSON.stringify({'prodID': prodId, 'quantity': quantityBought, 'bill': total, 'fName': fName, 'lName': lName, 'apt': apt, 'street': street, 'city': city, 'state': state, 'zipcode': zipcode, 'country': country, 'email': email, 'phone': phone})
            })
            return response.json();
          }

            createOrder: function(data, actions) {
              // This function sets up the details of the transaction, including the amount and line item details.
              return actions.order.create({
                purchase_units: [{
                  amount: {
                    value: '0.50' /*total*/
                  }
                }]
              });
            },
              onApprove: function(data, actions) {
                // This function captures the funds from the transaction.
                return actions.order.capture().then(function(details) {
                  // This function shows a transaction success message to your buyer.
                  completeOrder()
                  .then( data => {
                    alert(data)
                  });
                  //alert(data)
                  window.location.href = "{% url 'order_successful' DATA=prod.id %}" 
                });
              }
            }).render('#paypal-button-container');
            //This function displays Smart Payment Buttons on your web page.
            
</script>

我的main.urls.py如下:

from django.conf.urls import include
from django.urls import path
from . import views

#path(name_displayed_in_url, rendering_function, tag_name)

urlpatterns = [
    path('', views.home, name='home'),
    path('all-products', views.all_products, name='all-products'),
    path('request-a-quote', views.request_quote, name = 'RequestQuote'),
    path('contact-us', views.contact_us, name='ContactUs'),
    path('about', views.about, name='about'),
    path('product-details/<int:ID>', views.prod_temp, name='prod_temp'),
    path('ContactUs', views.ContactUs, name='contact-us'),
    path('calcQuote', views.calcQuote, name='calculate-quote'),
    path('product-details/purchase', views.purchase, name='purchase'),
    path('product-details/payment', views.payment, name='payment'),
    path('product-details/paymentComplete', views.paymentComplete, name='paymentComplete'),
    path('order_success/<int:DATA>', views.order_successful, name='order_successful'),
]

以下是我的 paymentComplete.views:


def paymentComplete(request):
    body = json.loads(request.body)

    prod_ID = int(body['prodID'])
    prod_Qt_bought = int(body['quantity'])
    bill_paid = str(body['bill'])

    #loading product from store
    prod_from_store = Product.objects.get(id=prod_ID)
    #previous product quantity    
    previous_Qt = int(prod_from_store.left)

    #amount to be left in store
    newLeft = previous_Qt - prod_Qt_bought


    return JsonResponse(prod_ID, safe=False)

和order_successful.view

def order_successful(request, DATA):

    return render(request, 'order_successful.html', {'DATA':DATA})

以下是 checkout.html 中的 completeOrder 函数,我在 views.py 中调用 paymentComplete 函数并向其发送 post 请求

**我已将问题缩小到 urls.py 文件,因为如果我在不传递任何参数的情况下调用 order_successful,我会成功获取我的页面,所以这要么是我感到困惑的错误方式关于 url 的编写,或者我在调用函数的 JS 脚本中做的错误。 **

我觉得你需要解决你的问题 - 涉及的事情太多,它们不适合错误消息。
您真的 post 文件的实际版本(urls.y ...)吗?

您的错误信息:

NoReverseMatch at /product-details/payment

Reverse for 'order_successful' with no arguments not found. 1 pattern(s) tried: ['order_success/(?P[^/]+)$']

我试着解释一下:

  1. 错误消息的第一行表示您调用了 url /product-details/payment Django 在那里找到
    a) 在视图中反向('order_successful') 或类似的
    b) 在视图的 html 标签中 {% url 'order_successful' % }
    并且因为没有通过异常给出额外的值。

因为你的urls.py包含

path('product-details/payment', views.payment, name='payment'),

我建议在 views.payment 和呈现的 html 中搜索(文件在这里不清楚,因为你不 post views.payment)。

  1. 您的错误消息的第二行说
.... tried: ['order_success/(?P[^/]+)$']

这很奇怪,因为您当前的 urls.py 没有导致此类错误消息的路径。 你的urls.py应该是

....tried: ['order_success/(?P<DATA>[0-9]+)$']

这就是为什么我要求您仔细检查文件,因为当前的不一致 error/related 文件无法为您指出解决方案。
So agian:你真的 post 文件的实际版本(urls.y ...)吗?

另一个提示:请在所有 html 中搜索 'order_successful'。如果你有一些带有 {% url 'order_successful' %} 的旧“评论”,django 会将其处理为 url 标记,只要你不将其包含在 django 特定评论中标记