我已经建立了一个 Django 项目,并将其与 Mopidy 的pyspotify 2.x 版集成。我编写了一段代码来检查 Spotify 会话实例,如果没有,则创建一个,然后从 Spotify URI 中获取曲目的名称。代码如下:
def spotify_logic(request):
if spotify.session_instance is not None:
foo = spotify.Track('spotify:track:7fHnHy7pH4hHLQxBq1NbE8')
foo.load()
name = foo.name
else:
config = spotify.SessionConfig()
config.application_key_filename = os.path.dirname(os.path.realpath(__file__)) + '/spotify_appkey.key'
session = spotify.Session(config=config)
session.login('TehNanor', '*******')
session.process_events()
foo = spotify.Track('spotify:track:7fHnHy7pH4hHLQxBq1NbE8')
foo.load()
name = foo.name
return render(request, 'clupus/spotify.html', {'name': name})
当我使用本地运行项目./manage.py runserver
并访问服务器时,它会显示歌曲的名称:“第 11 阶段 - 直到我们遇见天空”。当我将更改拉到我的 Apache 服务器上并访问 URL 时,我得到以下回溯。
Environment:
Request Method: GET
Request URL: http://ec2-54-196-205-226.compute-1.amazonaws.com/spotify/
Django Version: 1.6.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'raudio')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/public_html/clupus.com/clupus/raudio/views.py" in spotify_logic
63. foo.load()
File "/usr/local/lib/python2.7/dist-packages/spotify/track.py" in load
66. return utils.load(self, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/spotify/utils.py" in load
126. raise RuntimeError('Session must be logged in to load objects')
Exception Type: RuntimeError at /spotify/
Exception Value: Session must be logged in to load objects
我已经尝试了十几种代码变体,并且在本地运行的每次迭代都不适用于生产。我只能想象这个问题是因为我./manage.py runserver
在本地使用并且在我的 Apache 服务器上有一个 mod_wsgi 方法,但是我很难弄清楚如何纠正这个问题。