未定义全局名称“reverse”

2024-05-12 13:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用django-paypal库在我的网站上使用贝宝支付。

example之后,我设置了paypal_dict并添加了表单。

paypal_dict = {
        "business": settings.PAYPAL_RECEIVER_EMAIL,
        "amount": "10000000.00",
        "item_name": "name of the item",
        "invoice": "unique-invoice-id",
        "notify_url": "https://www.example.com" + reverse('paypal-ipn'),
        "return_url": "https://www.example.com/your-return-location/",
        "cancel_return": "https://www.example.com/your-cancel-location/",

    }

但是,我得到了错误global name 'reverse' is not defined我使用的是Python 2.7.9,发生了什么?


Tags: namehttpscomurlyourreturnexamplewww
3条回答

您需要导入函数reverse

from django.core.urlresolvers import reverse

你可以阅读更多关于它的资料。这是django特有的,但看起来你无论如何都在尝试构建一个URL,所以这可能是你想要的。

对于Django>;2.0

from django.urls import reverse

在Django2.0中:

from django.urls import reverse

reverse不是python中的内置函数。据推测,它是某个web框架中用于执行反向路由(从名称获取url路径)的函数。notify_url必须是Paypal将向其发送通知的应用程序中的url。

相关问题 更多 >