Я не могу напечатать переменную в моем emotion.html - PullRequest
0 голосов
/ 16 июня 2019

Я довольно новичок в python, и я обнаруживаю эмоции музыки, используя spotify api, но когда я передаю значения словаря в html, он ничего не печатает. тогда как я могу печатать в командной строке,

Когда я пишу {{энергия}} Ничего не печатается при доступе к localhost: 8000 // emotion

Это моя функция в views.py

    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_querry = title+ ' ' + artist
    result = sp.search(search_querry)
    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 в шаблонах

{{ energy }}

А в emotion_view.html ничего не печатает, что с ним не так

1 Ответ

0 голосов
/ 16 июня 2019

Измените {{energy}} на {{error}} в вашем emotion_view.html, так как вы передаете 'error' в своем словаре данных, а не 'energy'

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...