我有一个在线下载的数据库文件,我正在尝试从该数据库中获取内容并将其显示在模板上。
以下是我视图中的代码。 " getAllTeams()"是对连接到数据库并获取内容并返回列表
的方法的调用from django.shortcuts import render_to_response
from django.shortcuts import render,HttpResponse
from myTest.models import *
import pdb
import myTest.models
databaseFile ="../database.sqlite"
def index(request):
return render(request,'myTest/home.html')
def teams(request):
teamsList = getAllTeams(databaseFile)
teamsList.sort(key= lambda t: t.team_name)
# print(teamsList)
context = {
'teamsList':teamsList,
}
return render(request,'myTest/teams.html',context)
以下是获取数据并返回对象列表的方法
def getAllTeams(databaseFile):
conn = create_connection(databaseFile)
cur = conn.cursor()
cur.execute("SELECT * FROM Team t, Team_Attributes ta where
t.team_api_id = ta.team_api_id GROUP BY team_long_name")
rows = cur.fetchall()
teamList = []
for row in rows:
team = Team(row[0], row[1], row[2],
row[3], row[4], row[5],
row[6], row[7], row[8],
row[9], row[10], row[11],
row[12], row[13],
row[14], row[15], row[16],
row[17], row[18], row[19],
row[20], row[21], row[22],
row[23], row[24],
row[25], row[26], row[27])
#print(team)
teamList.append(team)
return teamList
This is the error I get when running the template
When I run the view in the console, the function works and returns the data
答案 0 :(得分:0)
我转到了设置文件,在数据库设置下,我更改了默认情况下数据库的名称,即' db.sqlite3' to' database.sqlite'这是我使用的外部数据库文件的名称。