15

I'm working on Braintree intergration in Django. I've followed this guide:

However, I'm getting the error 'str' object has no attribute 'get'.

Views.py

from django.shortcuts import render, render_to_response
from django.http.response import HttpResponse
import braintree
braintree.Configuration.configure(braintree.Environment.Sandbox,
                              "privatekey here",
                              "merchant key here",
                              "public key here")

def form(request):
    return render_to_response('braintree.html')

def create_transaction(request):
    if request.method == 'POST':
        print request.POST.get("number")       
        result = braintree.Transaction.sale({
         "amount": "1000.00",
         "credit_card": {
        "number": request.POST.get("number"),
        "cvv": request.POST.get("cvv"),
        "expiration_month": request.POST.get("month"),
        "expiration_year": request.POST.get("year")
    },
    "options": {
        "submit_for_settlement": True
    }
})

        if result.is_success:
            return "<h1>Success! Transaction ID: {0}</h1>".format(result.transaction.id)
        else:            
            return "<h1>Error: {0}</h1>".format(result.message)
    else:      
        return HttpResponse('no post')

The exception is

Environment:


Request Method: POST
Request URL: http://lcoalhost/create_transaction

Django Version: 1.6.2
Python Version: 2.7.5
Installed Applications:
 ('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in   get_response 201.response = middleware_method(request, response)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/clickjacking.py" in process_response 30.  if response.get('X-Frame-Options', None) is not None:

Exception Type: AttributeError at /create_transaction
Exception Value: 'str' object has no attribute 'get'
user2224250
  • 251
  • 1
  • 8
  • 19
  • 1
    What is the *full* traceback of the exception? In the Django error view, do click on the link to get the textual view of the traceback. – Martijn Pieters Mar 21 '14 at 12:58
  • http://dpaste.com/1750746/ – user2224250 Mar 21 '14 at 13:24
  • I work at Braintree. If the below answer doesn't solve your problem, you can always [reach out to our support team](https://support.braintreepayments.com/) for more help. – agf Mar 21 '14 at 16:09
  • @agf: if possible then can you send me better documentation than in the official website – user2224250 Mar 21 '14 at 17:11
  • @user2224250 In this case, the problem looks to be caused by a difference between Django and Flask (we use Flask in our guides) -- in Flask, you can return strings from your views; in Django, you need to return `HTTPResponse` objects. If you have more problems, our support team (reachable at the URL in my other comment) should be able to help. – agf Mar 21 '14 at 17:42

2 Answers2

31

You're returning strings directly from create_transaction inside the POST block. You need to wrap them in an HttpResponse.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
-3

open clickjacking.py of django middleware and change the following code

first import in what way you have to give response 
like in my case I have to return httpredirect so i import 
from django.http import HttpResponseRedirect
if isinstance(response, str):
        response = HttpResponseRedirect(response)
add this code into process_response of clickjacking.py
iqbal
  • 67
  • 5