global name 'reverse' is not defined
Asked Answered
W

7

15

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

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

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?

Whatnot answered 27/5, 2015 at 1:47 Comment(5)
You want to reverse the string?Gravante
I'm honestly not sure what it's trying to accomplish, that came straight from the example and trying to use it raises the error.Whatnot
There is no builtin function named reverse. Did you define one somewhere? Or expect to have imported one?Preteritive
Where's the traceback? The offensive code?Sandpaper
Presumably this is supposed to be the Django reverse function, which you get for free if your module is loaded as a Django view, but not if you import or run it in some other way.Preteritive
E
48

You need to import the function reverse:

For Django 2.0 and up:

from django.urls import reverse

For older Django:

from django.core.urlresolvers import reverse

It's specific to django, but it looks like you're trying to build a URL anyway, so it's probably what you want.

Enplane answered 27/5, 2015 at 1:59 Comment(0)
G
7

in Django2.0 :

from django.urls import reverse
Gang answered 29/5, 2018 at 18:18 Comment(0)
A
4

--Use this code in models.py......

from django.urls import reverse

def get_absolute_url(self):
    return reverse('url-name', kwargs={'pk':self.pk})
Alcahest answered 4/12, 2020 at 4:31 Comment(0)
S
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.

Sludge answered 27/5, 2015 at 1:52 Comment(0)
P
1

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

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.

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.)

Preteritive answered 27/5, 2015 at 1:55 Comment(0)
L
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.

from django.core.urlresolvers import reverse
Legislation answered 27/5, 2015 at 2:1 Comment(0)
K
0

I followed this solution to "reverse not defined" problem, and I still had this mistake... I imported reverse in all my urls files, with no effect... Then I imported reverse into views.py files and my local server started to work... The original solution to this message is right, but author didn't mention where to post import reverse, so I thought may be I will complete the original answer... If somehow I am wrong, I apologize, but it worked for me...

Kraemer answered 9/4, 2020 at 11:16 Comment(1)
Your answer must be formatted to understand. like problem and then solution. thanksBuss

© 2022 - 2024 — McMap. All rights reserved.