我試圖在類似的django項目中實現mailchimp python API,他們的示例在github上。我試圖讓基於類視圖連接然而,當我打開了我得到Mailchimp python包裝給出錯誤 - 沒有會話
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('/')