'str'对象没有属性'get' [英] 'str' object has no attribute 'get'
本文介绍了'str'对象没有属性'get'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Django中进行Braintree集成.我遵循了本指南:
I'm working on Braintree intergration in Django. I've followed this guide:
但是,出现错误'str'对象没有属性'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'
推荐答案
您是直接从POST块中的 create_transaction
返回字符串.您需要将它们包装在HttpResponse中.
You're returning strings directly from create_transaction
inside the POST block. You need to wrap them in an HttpResponse.
这篇关于'str'对象没有属性'get'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文