全局名称'reverse'未定义

时间:2022-10-16 18:09:36

I'm using the django-paypal library to use PayPal payments on my site.

我正在使用django-paypal库在我的网站上使用PayPal付款。

Following the example, I have set the paypal_dict and added the form.

在示例之后,我设置了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/",

    }

However, I am getting the error global name 'reverse' is not defined I am using Python 2.7.9, what's going on?

但是,我得到的错误全局名称'反向'未定义我正在使用Python 2.7.9,发生了什么?

5 个解决方案

#1


22  

You need to import the function reverse:

您需要导入反向函数:

from django.core.urlresolvers import reverse

You can read more about it here. It's specific to django, but it looks like you're trying to build a URL anyway, so it's probably what you want.

你可以在这里读更多关于它的内容。这是django特有的,但看起来你正试图建立一个URL,所以它可能就是你想要的。

#2


2  

in Django2.0 :

在Django2.0中:

from django.urls import reverse

#3


1  

reverse isn't a builtin function in python. Presumably, it's a function in some web framework for doing reverse-routing (getting a url path from a name). The notify_url has to be a url in your application that Paypal will send notices to.

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

#4


1  

There is no builtin function reverse in Python. (There is a reversed, but I doubt it's what you want.)

Python中没有内置函数反转。 (有一个相反,但我怀疑这是你想要的。)

There is a reverse function in Django. But you only get Django builtins in code that's loaded as a Django view or similar; if you import or run this code in some other way, it won't exist.

Django中有一个反向函数。但是你只能在加载为Django视图或类似代码的代码中获得Django内置函数;如果您以其他方式导入或运行此代码,它将不存在。

So, presumably, you've gotten something wrong earlier in the instructions, and aren't actually creating a view. (The Django-PayPal instructions are clearly written for someone who's already an experienced Django developer; if you don't understand basic Django concepts you will probably need to work through the tutorials first.)

因此,据推测,您在指令的前面已经出现了问题,并且实际上并没有创建视图。 (Django-PayPal指令清楚地写给已经是经验丰富的Django开发人员的人;如果你不理解基本的Django概念,你可能需要先完成这些教程。)

#5


1  

The url for paypal-ipn is probably defined in django-paypal's urls. I guess that importing django's reverse will solve this problem.

paypal-ipn的网址可能是在django-paypal的网址中定义的。我想导入django的反向将解决这个问题。

from django.core.urlresolvers import reverse

#1


22  

You need to import the function reverse:

您需要导入反向函数:

from django.core.urlresolvers import reverse

You can read more about it here. It's specific to django, but it looks like you're trying to build a URL anyway, so it's probably what you want.

你可以在这里读更多关于它的内容。这是django特有的,但看起来你正试图建立一个URL,所以它可能就是你想要的。

#2


2  

in Django2.0 :

在Django2.0中:

from django.urls import reverse

#3


1  

reverse isn't a builtin function in python. Presumably, it's a function in some web framework for doing reverse-routing (getting a url path from a name). The notify_url has to be a url in your application that Paypal will send notices to.

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

#4


1  

There is no builtin function reverse in Python. (There is a reversed, but I doubt it's what you want.)

Python中没有内置函数反转。 (有一个相反,但我怀疑这是你想要的。)

There is a reverse function in Django. But you only get Django builtins in code that's loaded as a Django view or similar; if you import or run this code in some other way, it won't exist.

Django中有一个反向函数。但是你只能在加载为Django视图或类似代码的代码中获得Django内置函数;如果您以其他方式导入或运行此代码,它将不存在。

So, presumably, you've gotten something wrong earlier in the instructions, and aren't actually creating a view. (The Django-PayPal instructions are clearly written for someone who's already an experienced Django developer; if you don't understand basic Django concepts you will probably need to work through the tutorials first.)

因此,据推测,您在指令的前面已经出现了问题,并且实际上并没有创建视图。 (Django-PayPal指令清楚地写给已经是经验丰富的Django开发人员的人;如果你不理解基本的Django概念,你可能需要先完成这些教程。)

#5


1  

The url for paypal-ipn is probably defined in django-paypal's urls. I guess that importing django's reverse will solve this problem.

paypal-ipn的网址可能是在django-paypal的网址中定义的。我想导入django的反向将解决这个问题。

from django.core.urlresolvers import reverse