Djano / Google Sheets API模型/查看问题

问题描述 投票:0回答:1

我对Python / Django很陌生:

我已经单独测试了Google Sheets API,并且可以正常工作。稍加修改,就将其放入模型中,如下所示:

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模板:

<html>
    <title></title>
    <head></head>
    <body>

        <h1>Spreadsheet</h1>

        {% for row in values %}
        <div>
            {% row %}
        </div>

    </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

我在这里做错了什么?有人可以指出正确的方向吗?非常感谢。

django python-3.x google-sheets-api
1个回答
0
投票

您在Jinja中缺少endfor标签

<html>
    <title></title>
    <head></head>
    <body>

        <h1>Spreadsheet</h1>

        {% for row in values %}
        <div>
            {% row %}
        </div>
        {% endfor %}  <!-- <<<<<<<<<<----------- Add this Line-->

    </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.