Search code examples
pythondjangopython-2.7paypalkeyerror

using django paypal getting error ('False', 'buy') KeyError at /


I think I am getting this error because I am adding a second key to the context variable and django paypal doesn't like it. But I don't really know to be honest. It renders ok with no error when I don't include the second key in the context variable and just leave {'form':form} like in the documentation. But I really need the 'document' object in my template and don't know how else to do it. Sorry I have no template added. I can never seem to paste them in here.

Any light on the problem is very appreciated.

Here is the view

def video(request, document_id): 
document = Document.objects.get(id=document_id)

if request.user.id:
    d1 =datetime.datetime.now().time()
    t=d1.strftime('%y%m%d%h%m%s')
    pp_price = str(document.price)
    # What you want the button to do.
    paypal_dict = {
        "business": settings.PAYPAL_RECEIVER_EMAIL,
        "amount": pp_price + ".00",
        "item_number1":document_id,
        "item_number2":request.user.id,
        "item_name": document.name,
        "invoice": document.name+t,
        "notify_url": "http://blabla.com/paid/" + reverse('paypal-ipn'),
        "return_url": "http://blabla.com/myvideos/",
        "cancel_return": "http://blabla.com/video/"+document_id+"/",
    }
    form = PayPalPaymentsForm(initial=paypal_dict)
    context = {"form": form, "document":document }
    return render(request, "video.html", context)
else:
    return render_to_response('video.html',{'document': document},
                              context_instance=RequestContext(request))

The exception thrown...

KeyError at /video/106/
('False', 'buy')
Request Method: GET
Request URL:    http://blabla.com/video/106/
Django Version: 1.6.4
Exception Type: KeyError
Exception Value:    
('False', 'buy')
Exception Location: /home/me/.local/lib/python2.7/site-packages/paypal/standard/forms.py in get_image, line 143
Python Executable:  /usr/local/bin/python
Python Version: 2.7.9
Python Path:    
['/home/me/webapps/blabla',
 '/home/me/webapps/blabla/src',
 '/home/me/webapps/blabla/lib/python2.7',
 '/home/me/lib/python2.7/pip-1.5.4-py2.7.egg',
 '/home/me/lib/python2.7',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/home/me/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages/PIL']
Server time:    Tue, 28 Apr 2015 17:19:04 +0000
Traceback Switch to copy-and-paste view

/home/me/.local/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                    response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/home/me/webapps/blabla/src/signups/views.py in video
        return render(request, "video.html", context) ...
▶ Local vars
/home/me/.local/lib/python2.7/site-packages/django/shortcuts/__init__.py in render
    return HttpResponse(loader.render_to_string(*args, **kwargs),

Solution

  • This is a bit of an obscure error to track down, but it actually has nothing to do with the code you show here. It appears that you have set one of your settings improperly. From the look of it, in your settings.py, you have:

    PAYPAL_TEST = 'True'
    

    When in fact, you should have:

    PAYPAL_TEST = True
    

    This variable needs to be a boolean, rather than a string.