Search code examples
pythondjangoapitrello

Using Trello API Key in a Django Project


I've checked Stackoverflow and I haven't been able to find a answer to my question, so I'm asking here.

I'm trying to create a dashboard which uses the Trello API in order to display specific metrics. The code section where I'm having trouble is here:

from django.conf import settings
from dashing.widgets import NumberWidget
from trello import TrelloApi

trello = TrelloApi(settings.API_KEY)

class TrelloCards(NumberWidget):
    title = 'The Difference Dashboard'
    def get_more_info(self):
        return ' {} closed'.format(len(trello.boards.get_card_filter('closed', 'BOARD_ID')))
    def get_change_rate(self):
        return ' {} open'.format(len(trello.boards.get_card_filter('closed', 'BOARD_ID')))
    def get_value(self):
        return len(trello.boards.get_card_filter('all', 'BOARD_ID'))

Where API_KEY is my Trello Developer API KEY and BOARD_ID is the id of the board I'm currently using.

When I go to run this code, I get an error message saying:

AttributeError at /dashboard/
'Settings' object has no attribute 'API_KEY'

I've tried changing the code to say

trello = TrelloApi(API_KEY)

In line with what the docs say, but this does not fix the issue.

If someone could just point me in the right direction with what I'm doing wrong, I'd be grateful!


Solution

  • You need to put these three credentials into settings.py file of your projects:

    Settings.py

    TRELLO_API_KEY = 'your_trello_key'
    TRELLO_API_SECRET = 'your_trello_api_secret'
    CALLBACK_DOMAIN = 'your_trello_callback_domain'
    

    Like this:

    import os
    
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        .....
        .....
      ]
    
    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        .....
        .....
    )
    
    API_KEY = 'your_trello_api_key'
    TRELLO_API_SECRET = 'your_trello_api_secret'
    CALLBACK_DOMAIN = 'your_trello_callback_domain'