首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我无法在emotion.html中打印变量

我无法在emotion.html中打印变量
EN

Stack Overflow用户
提问于 2019-06-16 21:41:37
回答 1查看 45关注 0票数 0

我是python的新手,我正在使用spotify api检测音乐的情感,但当我将字典值传递给html时,它什么也不打印。而我可以在命令行中打印,

当我写{{ energy }}时,当我访问localhost:8000//emotion时,它不打印任何内容

这是我在views.py中的函数

代码语言:javascript
复制
def emotion(request):
from mutagen.id3 import ID3
from mutagen.mp3 import MP3
import sys
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import requests
import spotify_token as st
import requests

client_id = '**********************'
client_secret = '*****************************'
title = 'lollypop'
artist = 'pawan singh'

client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sp.trace=False
search_query = title+ ' ' + artist
result = sp.search(search_query)
for i in result['tracks']['items']:
    # Find a songh that matches title and artist
    if (i['artists'][0]['name'] == artist) and (i['name'] == title):
        print (i['uri'])
        break
else:
    try:
        # Just take the first song returned by the search (might be named differently)
        print (result['tracks']['items'][0]['uri'])
        uri = result['tracks']['items'][0]['uri']
        client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
        sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
        sp.trace=False
        features = sp.audio_features(uri)

        data = {
            'energy' : features[0]['energy'],
            'key' : features[0]['key'],
            'valence': features[0]['valence'],
            'danceability': features[0]['danceability'],
            'loundness' : features[0]['loudness'],
            'tempo' : round(features[0]['tempo']),
            'acousticness' : features[0]['acousticness'],
            'liveness' : features[0]['liveness'],
            'instrumentalness' : features[0]['instrumentalness'],
        }

        print(features[0]['energy'])  # Prints successfully 0.862
        
    except:
        data = { 'error' : 'NO SONGS FOUND' }
return render(request, 'emotion_view.html', data)

这是我在模板中的emotion_view.html

代码语言:javascript
复制
{{ energy }}

在emotion_view.html中,它不打印任何内容,这有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2019-06-16 22:56:38

emotion_view.html中将{{energy}}更改为{{error}},因为在数据字典中传递的是'error'而不是'energy'

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56619384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档