Я очень плохо знаком с Python / Django:
Я тестировал API Google Sheets самостоятельно, и он работает. Слегка изменив его, я поместил его в свою модель следующим образом:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
def mainLoop():
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
#####################################################
## REMOVED THESE LINES FROM SAMPLE CODE FROM GOOGLE
#####################################################
#if not values:
# print('No data found.')
#else:
# print('Name, Major:')
# for row in values:
# Print columns A and E, which correspond to indices 0 and 4.
# print('%s, %s' % (row[0], row[4]))
#if __name__ == '__main__':
# main()
И я поместил следующее в моем представлении:
from django.shortcuts import render
from django.views import generic
from django.http import HttpResponse
from .models import mainLoop
from django.views.generic import ListView
def index2(ListView):
model = mainLoop
return render('', 'index.html')
А вот мой html template:
<html>
<title></title>
<head></head>
<body>
<h1>Spreadsheet</h1>
{% for row in values %}
<div>
{% row %}
</div>
{% endfor %}
</body>
</html>
И я получаю следующую ошибку:
TemplateSyntaxError at /
Invalid block tag on line 19: 'row', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 2.2
Exception Type: TemplateSyntaxError
Exception Value:
Invalid block tag on line 19: 'row', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Exception Location: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/template/base.py in invalid_block_tag, line 522
Python Executable: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3
Python Version: 3.8.2
Python Path:
['/Users/fred/Documents/googlypie/apisheets',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Users/fred/Library/Python/3.8/lib/python/site-packages',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']
Server time: Fri, 3 Apr 2020 06:49:49 +0000
Что я здесь не так делаю? Может кто-нибудь указать мне правильное направление? Большое спасибо.