如何在变量中存储多个下拉列表选择,并在Django中使用它们从Google Spreadsheet中获取数据?

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

我正在创建一个网站,要求用户选择科目、级别和媒介,并根据用户的选择返回资源(存储在谷歌电子表格中)。

当用户打开网站时,欢迎他们的是一个主页和一个开始的链接。点击链接后,就会进入SubjectPage,提示用户从下拉菜单中选择主题。提交选科后,会进入LevelPage,要求用户选择专业水平。提交级别选择后,会进入最后一个问题,要求用户选择自己喜欢的资源媒介。最后,用户会被带到 "结果页",在这里他们会得到一个资源列表。

到目前为止,我已经设置了我的页面,并将我的项目连接到我正在使用的谷歌电子表格。但是,我很难想出如何在页面之间携带用户的选择,并使用用户的三个选择在ResultsPage上显示信息。下面,我已经包含了截图和代码,可能会有所帮助。请随时向我进一步说明或更多截图。如有任何指导或建议,将非常感谢!

视图.py, SubjectPage.html, ResultsPage.html, urls.py, 电子表格.py

视图.py

from django.shortcuts import render
from django.http import HttpResponse

# Display HomePage
def HomePage(request):
    return render(request, 'ResourceApp/HomePage.html')

# Display SubjectPage
def SubjectPage(request):
    return render(request, 'ResourceApp/SubjectPage.html')

# Display LevelPage and carry over selection from SubjectPage
def LevelPage(request):
    subject = request.GET.get('SubjectSelection')
    response = render(request, 'ResourceApp/LevelPage.html')
    response.set_cookie('SubjectCookie', subject)
    return response

# Display MediumPage and carry over selection from LevelPage + Subject Page
def MediumPage(request):
    level = request.GET.get('LevelSelection')
    response = render(request, 'ResourceApp/MediumPage.html')
    response.set_cookie('LevelCookie', level)
    return response

# Fetch resources from Google Sheet based on three selections and return them on ResultsPage
def ResultsPage(request):
    medium = request.GET.get('MediumSelection')
    response = render(request, 'ResourceApp/ResultsPage.html')
    response.set_cookie('MediumCookie', medium)
    results = request.COOKIES.get('SubjectCookie') + request.COOKIES.get('LevelCookie') + request.COOKIES.get('MediumCookie')
    return render(request, 'ResourceApp/ResultsPage.html', results)

SubjectPage.html

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Subject | Tools For Fools</title>
</head>
<body>

<h1> Step 1 </h1>
<form action="http://127.0.0.1:8000/level/">
  <label for="Subject">Choose a Subject:</label>
  <select id="Subject" name="SubjectSelection">
    <option value="Knife Skills">Knife Skills</option>
    <option value="Culinary Terms">Culinary Terms</option>
    <option value="Recipes">Recipes</option>
  </select>
  <input type="Submit">
</form>

</body>
</html>

ResultsPage.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Results | Tools for Fools</title>
</head>
<body>
The Selected Items Are: {{results}}
</body>
</html>

电子表格.py

import gspread
from oauth2client.service_account import ServiceAccountCredentials

# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
sheet = client.open("Resource Project").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
python html django gspread
© www.soinside.com 2019 - 2024. All rights reserved.