web-dev-qa-db-ja.com

'str'オブジェクトには属性がありません 'get'

私はDjangoでBraintreeの統合に取り組んでいます。私は従った このガイド

ただし、エラー'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')

例外は

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'
12
user2224250

POSTブロック内のcreate_transactionから直接文字列を返します。それらをHttpResponseでラップする必要があります。

20
Daniel Roseman