【发布时间】:2015-05-15 19:19:51
【问题描述】:
我正在尝试在类似的 django 项目中实现 mailchimp python API,遵循他们在 github 上的示例。我试图在基于类的视图中建立连接,但是当我加载视图时,我收到了通知
Attribute Error at\
'module' object has no attribute 'session'
它的设置与他们的示例完全相同,错误发生在我定义的地方
m = get_mailchimp_api()
我在跟踪回溯后打开了我的站点包中的 mailchimp.py 文件并看到以下内容:
import requests
class Mailchimp(object):
root = 'https://api.mailchimp.com/2.0/'
def __init__(self, apikey=None, debug=False):
'''Initialize the API client
Args:
apikey (str|None): provide your MailChimp API key. If this is left as None, we will attempt to get the API key from the following locations::
- MAILCHIMP_APIKEY in the environment vars
- ~/.mailchimp.key for the user executing the script
- /etc/mailchimp.key
debug (bool): set to True to log all the request and response information to the "mailchimp" logger at the INFO level. When set to false, it will log at the DEBUG level. By default it will write log entries to STDERR
'''
self.session = requests.session()
回溯在self.session = requests.session() 行结束。
这是我试图调用 Mailchimp 的视图
from app.utils import get_mailchimp_api
import mailchimp
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = 'home.html'
# print requests -- this is undefined
m = get_mailchimp_api()
是不是因为 CBV 没有请求参数?在 github 示例中,它们显示了在函数接受请求的基于函数的视图中建立的连接。如果是这种情况,我如何将响应传递给 CBV?这是 Mailchimp 在 github 上给出的确切示例:
def index(request):
try:
m = get_mailchimp_api()
lists = m.lists.list()
except mailchimp.Error, e:
messages.error(request, 'An error occurred: %s - %s' % (e.__class__, e))
return redirect('/')
【问题讨论】:
标签: python django session mailchimp