skip to Main Content

I have data stored in Firestore and i would like to get data from a collection, filter it and publish it in HTML template.

I am using Django as the framework.

VIEWS.py

from django.shortcuts import render
import pyrebase
from firebase_admin import firestore
import datetime

db = firestore.Client()

config = {
    "apiKey": "xxxxxx",
    "authDomain": "xxxxxx.firebaseapp.com",
    "databaseURL": "https://xxxxxx.firebaseio.com",
    "projectId": "xxxxxx",
    "storageBucket": "xxxxxx.appspot.com",
    "messagingSenderId": "xxxxxx",
    "appId": "xxxxxx",
    "measurementId": "xxxxxx",
    "serviceAccount": "xxxxxx.json",
    }

# DATABASE
firebase = pyrebase.initialize_app(config)
authe = firebase.auth()
database = firebase.database()
print(database)

# TIME & DATE
today_date = datetime.datetime.now()
tomorrow_date = today_date + datetime.timedelta(days=1)
games_today = today_date.strftime("%Y-%m-%d")
games_tomorrow = tomorrow_date.strftime("%Y-%m-%d")
print(games_today)
print(games_tomorrow)


# NBA EVENT DATA
def xxxxx_basketball_nba_events(request):
    nba_events = db.collection('xxxx_au').document('basketball_nba').collection('event_info').stream()
    event_info = [doc.to_dict() for doc in nba_events]

    nba_games = sorted(event_info, key=lambda k: k['event_start'], reverse=True)
    # print(nba_games)

    for nba_game in nba_games:
        if nba_game['event_start'][:10] == games_tomorrow:
            event_id = nba_game['event_id']
            event_name = nba_game['event_name']
            event_status = nba_game['event_status']
            competition = nba_game['competition']
            event_start = nba_game['event_start'][:10]
            timestamp = nba_game['timestamp']

            print(event_id, event_name, event_status, competition, event_start, timestamp)

            data = ({
                u'event_id': event_id,
                u'event_name': event_name,
                u'event_status': event_status,
                u'competition': competition,
                u'event_start': event_start,
                u'timestamp': timestamp,
            })

            return render(request, 'html/nba.html', {'nba_games': data})

nba_games variable content

[{'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 195000, tzinfo=datetime.timezone.utc), 'event_name': 'Los Angeles Lakers - Portland Trail Blazers', 'event_id': 1018936256, 'event_status': 'NOT_STARTED', 'event_start': '2022-12-01T03:30:00Z', 'competition': 'NBA'}, {'event_start': '2022-12-01T03:00:00Z', 'event_name': 'Sacramento Kings - Indiana Pacers', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 175000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936251}, {'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 130000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936209, 'event_start': '2022-12-01T02:00:00Z', 'event_name': 'Phoenix Suns - Chicago Bulls', 'competition': 'NBA'}, {'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 148000, tzinfo=datetime.timezone.utc), 'event_start': '2022-12-01T02:00:00Z', 'event_name': 'Utah Jazz - Los Angeles Clippers', 'event_status': 'NOT_STARTED', 'competition': 'NBA', 'event_id': 1018936229}, {'event_id': 1018936241, 'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 110000, tzinfo=datetime.timezone.utc), 'event_start': '2022-12-01T02:00:00Z', 'event_status': 'NOT_STARTED', 'competition': 'NBA', 'event_name': 'Denver Nuggets - Houston Rockets'}, {'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 92000, tzinfo=datetime.timezone.utc), 'event_name': 'Oklahoma City Thunder - San Antonio Spurs', 'event_start': '2022-12-01T01:00:00Z', 'event_status': 'NOT_STARTED', 'event_id': 1018936233, 'competition': 'NBA'}, {'event_id': 1018936246, 'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 53000, tzinfo=datetime.timezone.utc), 'event_start': '2022-12-01T01:00:00Z', 'event_status': 'NOT_STARTED', 'competition': 'NBA', 'event_name': 'Minnesota Timberwolves - Memphis Grizzlies'}, {'event_name': 'New Orleans Pelicans - Toronto Raptors', 'event_id': 1018936258, 'event_start': '2022-12-01T01:00:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 76000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'competition': 'NBA'}, {'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 33000, tzinfo=datetime.timezone.utc), 'event_id': 1018936245, 'event_name': 'New York Knicks - Milwaukee Bucks', 'event_start': '2022-12-01T00:41:56Z', 'event_status': 'STARTED'}, {'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 2, 15000, tzinfo=datetime.timezone.utc), 'event_start': '2022-12-01T00:40:58Z', 'event_name': 'Boston Celtics - Miami Heat', 'event_status': 'STARTED', 'competition': 'NBA', 'event_id': 1018936268}, {'competition': 'NBA', 'event_status': 'STARTED', 'event_id': 1018936243, 'event_start': '2022-12-01T00:40:43Z', 'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 1, 996000, tzinfo=datetime.timezone.utc), 'event_name': 'Brooklyn Nets - Washington Wizards'}, {'competition': 'NBA', 'event_id': 1018936226, 'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 1, 978000, tzinfo=datetime.timezone.utc), 'event_start': '2022-12-01T00:10:25Z', 'event_name': 'Cleveland Cavaliers - Philadelphia 76ers', 'event_status': 'STARTED'}, {'event_name': 'Orlando Magic - Atlanta Hawks', 'event_status': 'STARTED', 'event_id': 1018936242, 'event_start': '2022-12-01T00:10:19Z', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 12, 1, 1, 0, 1, 960000, tzinfo=datetime.timezone.utc)}, {'event_start': '2022-11-30T03:00:00Z', 'event_name': 'Portland Trail Blazers - Los Angeles Clippers', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 29, 19, 30, 5, 436000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936272}, {'event_id': 1018936236, 'event_start': '2022-11-30T00:30:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 29, 19, 30, 5, 419000, tzinfo=datetime.timezone.utc), 'event_name': 'Dallas Mavericks - Golden State Warriors', 'competition': 'NBA', 'event_status': 'NOT_STARTED'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 29, 19, 30, 5, 403000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936230, 'event_start': '2022-11-30T00:00:00Z', 'event_name': 'Detroit Pistons - New York Knicks', 'competition': 'NBA'}, {'event_id': 1018936255, 'event_start': '2022-11-29T03:30:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 681000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_name': 'Los Angeles Lakers - Indiana Pacers', 'event_status': 'NOT_STARTED'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 664000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936259, 'event_start': '2022-11-29T03:00:00Z', 'event_name': 'Sacramento Kings - Phoenix Suns', 'competition': 'NBA'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 619000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_id': 1018936225, 'event_status': 'NOT_STARTED', 'event_start': '2022-11-29T02:00:00Z', 'event_name': 'Denver Nuggets - Houston Rockets'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 635000, tzinfo=datetime.timezone.utc), 'event_name': 'Utah Jazz - Chicago Bulls', 'event_status': 'NOT_STARTED', 'event_start': '2022-11-29T02:00:00Z', 'event_id': 1018936240, 'competition': 'NBA'}, {'event_name': 'New Orleans Pelicans - Oklahoma City Thunder', 'event_id': 1018936249, 'event_start': '2022-11-29T01:00:00Z', 'event_status': 'NOT_STARTED', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 583000, tzinfo=datetime.timezone.utc)}, {'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 569000, tzinfo=datetime.timezone.utc), 'event_id': 1018936264, 'event_name': 'Toronto Raptors - Cleveland Cavaliers', 'event_start': '2022-11-29T00:30:00Z', 'event_status': 'NOT_STARTED'}, {'event_start': '2022-11-29T00:30:00Z', 'event_name': 'Boston Celtics - Charlotte Hornets', 'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 492000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_id': 1018936266, 'event_status': 'NOT_STARTED'}, {'event_status': 'NOT_STARTED', 'competition': 'NBA', 'event_name': 'Brooklyn Nets - Orlando Magic', 'event_id': 1018936275, 'event_start': '2022-11-29T00:30:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 533000, tzinfo=datetime.timezone.utc)}, {'event_name': 'Philadelphia 76ers - Atlanta Hawks', 'event_id': 1018936215, 'event_start': '2022-11-29T00:00:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 414000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'competition': 'NBA'}, {'event_id': 1018936237, 'event_status': 'NOT_STARTED', 'event_start': '2022-11-29T00:00:00Z', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 28, 19, 30, 5, 463000, tzinfo=datetime.timezone.utc), 'event_name': 'Washington Wizards - Minnesota Timberwolves'}, {'competition': 'NBA', 'event_name': 'Milwaukee Bucks - Dallas Mavericks', 'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 585000, tzinfo=datetime.timezone.utc), 'event_id': 1018936297, 'event_start': '2022-11-28T01:00:00Z', 'event_status': 'NOT_STARTED'}, {'event_id': 1018936274, 'event_start': '2022-11-27T23:00:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 524000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_name': 'Boston Celtics - Washington Wizards', 'event_status': 'NOT_STARTED'}, {'event_id': 1018936279, 'event_start': '2022-11-27T23:00:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 570000, tzinfo=datetime.timezone.utc), 'event_name': 'Orlando Magic - Philadelphia 76ers', 'competition': 'NBA', 'event_status': 'NOT_STARTED'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 540000, tzinfo=datetime.timezone.utc), 'event_start': '2022-11-27T23:00:00Z', 'event_name': 'Detroit Pistons - Cleveland Cavaliers', 'competition': 'NBA', 'event_status': 'NOT_STARTED', 'event_id': 1018936306}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 555000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936310, 'event_start': '2022-11-27T23:00:00Z', 'event_name': 'New York Knicks - Memphis Grizzlies', 'competition': 'NBA'}, {'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 508000, tzinfo=datetime.timezone.utc), 'event_id': 1018936287, 'event_status': 'NOT_STARTED', 'event_start': '2022-11-27T22:00:00Z', 'event_name': 'Atlanta Hawks - Miami Heat'}, {'event_name': 'Los Angeles Clippers - Indiana Pacers', 'event_status': 'NOT_STARTED', 'event_id': 1018936294, 'event_start': '2022-11-27T21:00:00Z', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 487000, tzinfo=datetime.timezone.utc)}, {'event_start': '2022-11-27T20:30:00Z', 'event_name': 'Minnesota Timberwolves - Golden State Warriors', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 465000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936267}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 27, 19, 30, 6, 448000, tzinfo=datetime.timezone.utc), 'event_name': 'Brooklyn Nets - Portland Trail Blazers', 'event_status': 'NOT_STARTED', 'event_id': 1018936300, 'event_start': '2022-11-27T20:00:00Z', 'competition': 'NBA'}, {'event_name': 'Phoenix Suns - Utah Jazz', 'event_id': 1018936305, 'event_start': '2022-11-27T02:00:00Z', 'event_status': 'NOT_STARTED', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 26, 19, 30, 6, 126000, tzinfo=datetime.timezone.utc)}, {'event_id': 1018936280, 'event_status': 'NOT_STARTED', 'event_start': '2022-11-27T01:00:00Z', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 26, 19, 30, 6, 80000, tzinfo=datetime.timezone.utc), 'event_name': 'Houston Rockets - Oklahoma City Thunder'}, {'competition': 'NBA', 'event_name': 'San Antonio Spurs - Los Angeles Lakers', 'event_id': 1018936288, 'timestamp': DatetimeWithNanoseconds(2022, 11, 26, 19, 30, 6, 106000, tzinfo=datetime.timezone.utc), 'event_start': '2022-11-27T01:00:00Z', 'event_status': 'NOT_STARTED'}, {'event_start': '2022-11-26T22:00:00Z', 'event_name': 'Toronto Raptors - Dallas Mavericks', 'timestamp': DatetimeWithNanoseconds(2022, 11, 26, 19, 30, 6, 57000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_id': 1018936289, 'event_status': 'NOT_STARTED'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 26, 4, 49, 39, 510000, tzinfo=datetime.timezone.utc), 'event_name': 'Los Angeles Clippers - Denver Nuggets', 'competition': 'NBA', 'event_status': 'STARTED', 'event_id': 1018936316, 'event_start': '2022-11-26T03:41:14Z'}, {'competition': 'NBA', 'event_status': 'STARTED', 'event_name': 'Golden State Warriors - Utah Jazz', 'event_id': 1018936308, 'event_start': '2022-11-26T03:10:44Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 26, 4, 49, 39, 417000, tzinfo=datetime.timezone.utc)}, {'event_start': '2022-11-26T02:00:00Z', 'event_name': 'Phoenix Suns - Detroit Pistons', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 448000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_id': 1018936262, 'event_status': 'NOT_STARTED'}, {'event_id': 1018936276, 'event_start': '2022-11-26T01:00:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 248000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_name': 'Houston Rockets - Atlanta Hawks', 'event_status': 'NOT_STARTED'}, {'competition': 'NBA', 'event_name': 'Indiana Pacers - Brooklyn Nets', 'event_id': 1018936285, 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 292000, tzinfo=datetime.timezone.utc), 'event_start': '2022-11-26T01:00:00Z', 'event_status': 'NOT_STARTED'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 433000, tzinfo=datetime.timezone.utc), 'event_start': '2022-11-26T01:00:00Z', 'event_name': 'San Antonio Spurs - Los Angeles Lakers', 'competition': 'NBA', 'event_status': 'NOT_STARTED', 'event_id': 1018936286}, {'competition': 'NBA', 'event_name': 'Miami Heat - Washington Wizards', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 338000, tzinfo=datetime.timezone.utc), 'event_id': 1018936295, 'event_start': '2022-11-26T01:00:00Z', 'event_status': 'NOT_STARTED'}, {'event_start': '2022-11-26T01:00:00Z', 'event_name': 'Boston Celtics - Sacramento Kings', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 230000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936302}, {'event_start': '2022-11-26T01:00:00Z', 'event_name': 'Oklahoma City Thunder - Chicago Bulls', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 407000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_id': 1018936319, 'event_status': 'NOT_STARTED'}, {'event_name': 'Milwaukee Bucks - Cleveland Cavaliers', 'event_id': 1018936322, 'event_start': '2022-11-26T01:00:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 375000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'competition': 'NBA'}, {'event_id': 1018936329, 'event_start': '2022-11-26T01:00:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 325000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_name': 'Memphis Grizzlies - New Orleans Pelicans', 'event_status': 'NOT_STARTED'}, {'event_start': '2022-11-26T00:30:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 199000, tzinfo=datetime.timezone.utc), 'event_name': 'New York Knicks - Portland Trail Blazers', 'competition': 'NBA', 'event_status': 'NOT_STARTED', 'event_id': 1018936292}, {'event_start': '2022-11-26T00:00:00Z', 'event_name': 'Orlando Magic - Philadelphia 76ers', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 177000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936265}, {'event_start': '2022-11-25T22:10:31Z', 'event_name': 'Charlotte Hornets - Minnesota Timberwolves', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 25, 22, 41, 18, 121000, tzinfo=datetime.timezone.utc), 'event_status': 'STARTED', 'event_id': 1018936312}, {'competition': 'NBA', 'event_status': 'STARTED', 'event_name': 'Golden State Warriors - Los Angeles Clippers', 'event_id': 1018936278, 'event_start': '2022-11-24T03:10:36Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 24, 4, 9, 50, 895000, tzinfo=datetime.timezone.utc)}, {'event_start': '2022-11-24T02:10:29Z', 'event_name': 'Utah Jazz - Detroit Pistons', 'timestamp': DatetimeWithNanoseconds(2022, 11, 24, 4, 9, 50, 831000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_id': 1018936303, 'event_status': 'STARTED'}, {'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 24, 2, 55, 28, 44000, tzinfo=datetime.timezone.utc), 'event_id': 1018936301, 'event_name': 'Oklahoma City Thunder - Denver Nuggets', 'event_start': '2022-11-24T01:10:18Z', 'event_status': 'STARTED'}, {'competition': 'NBA', 'event_id': 1018936299, 'timestamp': DatetimeWithNanoseconds(2022, 11, 24, 2, 55, 28, 8000, tzinfo=datetime.timezone.utc), 'event_start': '2022-11-24T01:10:16Z', 'event_name': 'Milwaukee Bucks - Chicago Bulls', 'event_status': 'STARTED'}, {'event_name': 'San Antonio Spurs - New Orleans Pelicans', 'event_id': 1018936281, 'event_start': '2022-11-24T01:10:15Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 24, 2, 55, 27, 968000, tzinfo=datetime.timezone.utc), 'event_status': 'STARTED', 'competition': 'NBA'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 24, 2, 55, 27, 923000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_id': 1018936284, 'event_status': 'STARTED', 'event_start': '2022-11-24T00:43:40Z', 'event_name': 'Miami Heat - Washington Wizards'}, {'competition': 'NBA', 'event_status': 'NOT_STARTED', 'event_name': 'Toronto Raptors - Brooklyn Nets', 'event_id': 1018936293, 'event_start': '2022-11-24T00:30:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 23, 16, 36, 183000, tzinfo=datetime.timezone.utc)}, {'event_start': '2022-11-24T00:30:00Z', 'event_name': 'Atlanta Hawks - Sacramento Kings', 'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 23, 16, 36, 75000, tzinfo=datetime.timezone.utc), 'competition': 'NBA', 'event_id': 1018936311, 'event_status': 'NOT_STARTED'}, {'event_status': 'NOT_STARTED', 'competition': 'NBA', 'event_name': 'Boston Celtics - Dallas Mavericks', 'event_id': 1018936314, 'event_start': '2022-11-24T00:30:00Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 23, 16, 36, 130000, tzinfo=datetime.timezone.utc)}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 23, 16, 36, 47000, tzinfo=datetime.timezone.utc), 'event_status': 'NOT_STARTED', 'event_id': 1018936282, 'event_start': '2022-11-24T00:00:00Z', 'event_name': 'Indiana Pacers - Minnesota Timberwolves', 'competition': 'NBA'}, {'event_name': 'Cleveland Cavaliers - Portland Trail Blazers', 'event_status': 'NOT_STARTED', 'event_id': 1018936296, 'event_start': '2022-11-24T00:00:00Z', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 23, 16, 35, 988000, tzinfo=datetime.timezone.utc)}, {'competition': 'NBA', 'event_id': 1018936309, 'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 23, 16, 35, 921000, tzinfo=datetime.timezone.utc), 'event_name': 'Charlotte Hornets - Philadelphia 76ers', 'event_start': '2022-11-24T00:00:00Z', 'event_status': 'NOT_STARTED'}, {'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 4, 42, 57, 664000, tzinfo=datetime.timezone.utc), 'event_status': 'STARTED', 'event_id': 1018936307, 'event_start': '2022-11-23T03:04:46Z', 'event_name': 'Phoenix Suns - Los Angeles Lakers', 'competition': 'NBA'}, {'event_start': '2022-11-23T02:11:59Z', 'event_name': 'Denver Nuggets - Detroit Pistons', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 3, 17, 19, 117000, tzinfo=datetime.timezone.utc), 'event_status': 'STARTED', 'event_id': 1018936291}, {'event_name': 'Memphis Grizzlies - Sacramento Kings', 'event_id': 1018936283, 'event_start': '2022-11-23T01:11:03Z', 'timestamp': DatetimeWithNanoseconds(2022, 11, 23, 3, 17, 19, 55000, tzinfo=datetime.timezone.utc), 'event_status': 'STARTED', 'competition': 'NBA'}, {'event_id': 1018936277, 'event_status': 'NOT_STARTED', 'event_start': '2022-11-23T00:30:00Z', 'competition': 'NBA', 'timestamp': DatetimeWithNanoseconds(2022, 11, 22, 22, 3, 18, 277000, tzinfo=datetime.timezone.utc), 'event_name': 'Philadelphia 76ers - Brooklyn Nets'}]

HTML template

{% block content %}
    
<table class="table table-striped" style="padding: 15px; width: 1000px">
<thead>
    <tr>
      <th scope="col">Event ID</th>
      <th scope="col">Competition</th>
      <th scope="col">Event Name</th>
      <th scope="col">Event Start</th>
      <th scope="col">Event Status</th>
    </tr>
  </thead>
{% for xxx_nba in data %}
  <tbody>
    <tr>
      <th>{{xxx_nba.event_id}}</th>
      <td>{{xxx_nba.competition}}</td>
      <td>{{xxx_nba.event_name}}</td>
      <td>{{xxx_nba.event_start}}</td>
      <td>{{xxx_nba.event_status}}</td>
    </tr>
  </tbody>
{% endfor %}
</table>

{% endblock %} 

HTML Output

1018936256  NBA Los Angeles Lakers - Portland Trail Blazers 2022-12-01T03:30:00Z    NOT_STARTED
1018936251  NBA Sacramento Kings - Indiana Pacers   2022-12-01T03:00:00Z    NOT_STARTED
1018936209  NBA Phoenix Suns - Chicago Bulls    2022-12-01T02:00:00Z    NOT_STARTED
1018936229  NBA Utah Jazz - Los Angeles Clippers    2022-12-01T02:00:00Z    NOT_STARTED
1018936241  NBA Denver Nuggets - Houston Rockets    2022-12-01T02:00:00Z    NOT_STARTED
1018936233  NBA Oklahoma City Thunder - San Antonio Spurs   2022-12-01T01:00:00Z    NOT_STARTED
1018936246  NBA Minnesota Timberwolves - Memphis Grizzlies  2022-12-01T01:00:00Z    NOT_STARTED
1018936258  NBA New Orleans Pelicans - Toronto Raptors  2022-12-01T01:00:00Z    NOT_STARTED
1018936245  NBA New York Knicks - Milwaukee Bucks   2022-12-01T00:41:56Z    STARTED
1018936268  NBA Boston Celtics - Miami Heat 2022-12-01T00:40:58Z    STARTED
1018936243  NBA Brooklyn Nets - Washington Wizards  2022-12-01T00:40:43Z    STARTED
1018936226  NBA Cleveland Cavaliers - Philadelphia 76ers    2022-12-01T00:10:25Z    STARTED
1018936242  NBA Orlando Magic - Atlanta Hawks   2022-12-01T00:10:19Z    STARTED
1018936272  NBA Portland Trail Blazers - Los Angeles Clippers   2022-11-30T03:00:00Z    NOT_STARTED
1018936236  NBA Dallas Mavericks - Golden State Warriors    2022-11-30T00:30:00Z    NOT_STARTED
1018936230  NBA Detroit Pistons - New York Knicks   2022-11-30T00:00:00Z    NOT_STARTED
1018936255  NBA Los Angeles Lakers - Indiana Pacers 2022-11-29T03:30:00Z    NOT_STARTED
1018936259  NBA Sacramento Kings - Phoenix Suns 2022-11-29T03:00:00Z    NOT_STARTED
1018936225  NBA Denver Nuggets - Houston Rockets    2022-11-29T02:00:00Z    NOT_STARTED
1018936240  NBA Utah Jazz - Chicago Bulls   2022-11-29T02:00:00Z    NOT_STARTED
1018936249  NBA New Orleans Pelicans - Oklahoma City Thunder    2022-11-29T01:00:00Z    NOT_STARTED
1018936264  NBA Toronto Raptors - Cleveland Cavaliers   2022-11-29T00:30:00Z    NOT_STARTED
1018936266  NBA Boston Celtics - Charlotte Hornets  2022-11-29T00:30:00Z    NOT_STARTED
1018936275  NBA Brooklyn Nets - Orlando Magic   2022-11-29T00:30:00Z    NOT_STARTED
1018936215  NBA Philadelphia 76ers - Atlanta Hawks  2022-11-29T00:00:00Z    NOT_STARTED
1018936237  NBA Washington Wizards - Minnesota Timberwolves 2022-11-29T00:00:00Z    NOT_STARTED
1018936297  NBA Milwaukee Bucks - Dallas Mavericks  2022-11-28T01:00:00Z    NOT_STARTED

I need to filter by event_start which is formatted like

2022-12-01T02:00:00Z

I am trying to reformat that date so that I can use 2022-12-01 section of the date data to filter the games for the day.

I would like to send data to the HTML for only the games that are in the database with the start time (event_start) matching the days date (games_today).

Any help would be apreciated in filtering this data by date and publishing in the HTML document.

2

Answers


  1. instead of creating a stream/snapshot of the event_info subCollection, why don’t you create a query snapshot/stream of the event_info subCollection which will only return data that satisfies your query filter only::

    I am inferring from this section of your code:

    nba_events = db.collection('xxxx_au').document('basketball_nba').collection('event_info').stream()
    event_info = [doc.to_dict() for doc in nba_events]
    

    This only returns all data for event_info sub-collection

    You can create a query stream/snapshot like this

    # Create an Event for notifying main thread.
    callback_done = threading.Event()
    
    # Create a callback on_snapshot function to capture changes
    def on_snapshot(col_snapshot, changes, read_time):
        print(u'Callback received query snapshot.')
        print(u'Current cities in California:')
        for doc in col_snapshot:
            print(f'{doc.id}')
        callback_done.set()
    
    col_query = db.collection(u'cities').where(u'state', u'==', u'CA')
    
    # Watch the collection query
    query_watch = col_query.on_snapshot(on_snapshot)
    

    Read more about it here: https://firebase.google.com/docs/firestore/query-data/listen#python_3

    Login or Signup to reply.
  2. I think you have to use date format of the following to filter the queries try :

    today_date = datetime.datetime.now()
    tomorrow_date = today_date + datetime.timedelta(days=1)
    games_today = today_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
    games_tomorrow = tomorrow_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
    print(games_today)   #will give you same date format that is stored in firestore
    print(games_tomorrow)
    
    

    then to filter based on the date as you have filtered.
    Then assign it to your collection then present it in your html.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search