python - 'str' 对象没有属性 'get'

标签 python django braintree

我正在研究 Django 中的 Braintree 集成。我关注了this guide :

但是,我收到错误消息 'str' object has no attribute 'get'

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

异常(exception)是

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'

最佳答案

您直接从 POST block 内的 create_transaction 返回字符串。您需要将它们包装在 HttpResponse 中。

关于python - 'str' 对象没有属性 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22559446/

相关文章:

paypal - 使用 braintree 生产帐户接受 paypal

python - 如何在cygwin上通过Python脚本调用文件

python - 在 Django 中有一种方法可以根据相关对象的条件聚合关系

python - 写入 CSV - 字符串在 Excel 中被识别为日期

django manytomany模型关系在对象创建时崩溃管理

django - 在 uWSGI 中启用内部路由

python - 在新的更大图像上复制图像

django - 在本地为 Django 使用 SQLite,在服务器上使用 Postgres

javascript - Braintree-javascript - 收集额外的客户信息?

java - Braintree 集成 - 错误 : Class not found: com. Braintreepayments.api.dropin.DropInRequest