Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global name 'reverse' is not defined

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?

like image 992
Magic Avatar asked May 27 '15 01:05

Magic


People also ask

What is global name not defined in Python?

NameError: global name '---' is not definedOther names are defined within the program (such as variables). If Python encounters a name that it doesn't recognize, you'll probably get this error. Some common causes of this error include: Forgetting to give a variable a value before using it in another statement.

How do I fix name not defined error in Python?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.


2 Answers

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.

like image 142
TankorSmash Avatar answered Sep 20 '22 18:09

TankorSmash


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

from django.urls import reverse

def get_absolute_url(self):
    return reverse('url-name', kwargs={'pk':self.pk})
like image 40
Harun Bansode Avatar answered Sep 20 '22 18:09

Harun Bansode