Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在flask中链接多个列表_Python_List_Flask - Fatal编程技术网

Python 在flask中链接多个列表

Python 在flask中链接多个列表,python,list,flask,Python,List,Flask,我正在尝试创建一个flask项目,根据您选择的团队显示特定的详细信息。例如,如果我点击名单上的第四支球队切尔西,我希望第四枚徽章、经理、球场、能力和昵称出现,但我不知道如何做到这一点 (目前,我只是让它输出每个列表中的第一项内容,以便在屏幕上显示某些内容) 足球 from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return render_template(

我正在尝试创建一个flask项目,根据您选择的团队显示特定的详细信息。例如,如果我点击名单上的第四支球队切尔西,我希望第四枚徽章、经理、球场、能力和昵称出现,但我不知道如何做到这一点

(目前,我只是让它输出每个列表中的第一项内容,以便在屏幕上显示某些内容)

足球

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
  return render_template('football.html')

@app.route('/bpl/')
def bplLink():
  teamName = ['AFC Bournemouth', 'Arsenal', 'Aston Villa', 'Chelsea',
  'Crystal Palace', 'Everton', 'Leicester City', 'Liverpool', 'Manchester City',
  'Manchester United', 'Newcastle United', 'Norwich City', 'Southampton',
  'Stoke City', 'Sunderland', 'Swansea City', 'Tottenham Hotspur',
  'Watford', 'West Bromwich Albion', 'West Ham United']
  return render_template('bplteams.html', teamName=teamName)

@app.route('/bpl/<team>')
def team(team):
  stadium = ['Dean Court', 'Emirates Stadium', 'Villa Park', 'Stamford Bridge',
  'Selhurst Park', 'Goodison Park', 'King Power Stadium', 'Anfield',
  'Etihad Stadium', 'Old Trafford', 'St James Park', 'Carrow Road',
  'St Marys Stadium', 'Britannia Stadium', 'Stadium of Light',
  'Liberty Stadium', 'White Hart Lane', 'Vicarage Road', 'The Hawthorns',
  'Boleyn Ground']
  badge = ['bournemouth.png', 'arsenal.png', 'aston-villa.png', 'chelsea.png',
  'crystal-palace.png', 'everton.png', 'leicester.png', 'liverpool',
  'man-city.png', 'man-united.png', 'newcastle.png', 'norwich.png',
  'southampton.png', 'stoke.png', 'sunderland.png', 'swansea.png',
  'tottenham.png', 'watford.png', 'west-brom.png', 'west-ham.png']
  capacity = ['11,464', '60,260', '42,660', '41,798', '25,073', '39,571',
  '32,312', '44,742', '55,097', '75,653', '52,338', '27,010', '32,505',
  '27,740', '48,707', '20,909', '36,284', '21,500', '26,850', '35,345']
  manager = ['Eddie Howe', 'Arsene Wenger', 'Kevin MacDonald (caretaker)',
  'Jose Mourinho', 'Alan Pardew', 'Roberto Martinez', 'Claudio Ranieri',
  'Jurgen Klopp', 'Manuel Pellegrini', 'Louis van Gaal', 'Steve McClaren',
  'Alex Neil', 'Ronald Koeman', 'Mark Hughes', 'Sam Allardyce', 'Garry Monk',
  'Mauricio Pochettino', 'Quique Flores', 'Tony Pulis', 'Slaven Bilic']
  nickname = ['Cherries', 'Gunners', 'Villans', 'Blues', 'Eagles', 'Toffees',
  'Foxes', 'Reds', 'City', 'Red Devils', 'Magpies', 'Canaries', 'Saints',
  'Potters', 'Black Cats', 'Swans', 'Spurs', 'Hornets', 'Baggies', 'Irons']
  return render_template('details.html', team=team, stadium=stadium,
  badge=badge, capacity=capacity, manager=manager, nickname=nickname)
从烧瓶导入烧瓶,渲染\u模板
app=烧瓶(名称)
@应用程序路径(“/”)
def index():
返回渲染模板('football.html')
@app.route(“/bpl/”)
def bplLink():
球队名称=['AFC伯恩茅斯','阿森纳','阿斯顿维拉','切尔西',
‘水晶宫’、‘埃弗顿’、‘莱斯特城’、‘利物浦’、‘曼城’,
“曼彻斯特联队”、“纽卡斯尔联队”、“诺维奇城”、“南安普敦”,
“斯托克城”、“桑德兰”、“斯旺西城”、“托特纳姆热刺”,
“沃特福德”、“西布朗维奇”、“西汉姆联队”]
返回渲染模板('bplteams.html',teamName=teamName)
@app.route(“/bpl/”)
def团队(团队):
体育场=[“迪恩球场”、“酋长球场”、“维拉公园”、“斯坦福桥”,
“塞尔赫斯特公园”、“古迪逊公园”、“国王权力体育场”、“安菲尔德”,
“埃提哈德体育场”、“老特拉福德”、“圣詹姆斯公园”、“卡罗路”,
“圣玛丽体育场”、“不列颠尼亚体育场”、“光明体育场”,
‘自由体育场’、‘白鹿巷’、‘牧师路’、‘山楂树’,
“博林球场”]
badge=['bournemouth.png'、'arsenal.png'、'aston villa.png'、'chelsea.png',
水晶宫、埃弗顿、莱斯特、利物浦、,
“man city.png”、“man united.png”、“newcastle.png”、“norwich.png”,
“southanpton.png”、“stoke.png”、“sunderland.png”、“swansea.png”,
“tottenham.png”、“watford.png”、“west brom.png”、“west ham.png”]
容量=['11464','60260','42660','41798','25073','39571',',
'32,312', '44,742', '55,097', '75,653', '52,338', '27,010', '32,505',
'27,740', '48,707', '20,909', '36,284', '21,500', '26,850', '35,345']
经理=['Eddie Howe','Arsene Wenger','Kevin MacDonald(看门人)',
“穆里尼奥”、“帕杜”、“马丁内斯”、“拉涅利”,
“Jurgen Klopp”、“Manuel Pellegrini”、“Louis van Gaal”、“Steve McClaren”,
“亚历克斯·尼尔”、“罗纳德·科曼”、“马克·休斯”、“山姆·阿勒代斯”、“加里·蒙克”,
“毛里西奥·波切蒂诺”、“奎克·弗洛雷斯”、“托尼·普利斯”、“斯拉文·比利奇”]
昵称=[‘樱桃’、‘枪手’、‘恶棍’、‘蓝调’、‘老鹰’、‘太妃糖’,
‘狐狸’、‘红色’、‘城市’、‘红魔’、‘喜鹊’、‘金丝雀’、‘圣徒’,
“波特队”、“黑猫队”、“天鹅队”、“马刺队”、“黄蜂队”、“袋鼠队”、“铁杆队”]
返回渲染模板('details.html',team=team,stadium=stadium,
徽章=徽章,容量=容量,经理=经理,昵称=昵称)
bplteams.html(列出所有团队)


{teamName%中团队的%s}

{%endfor%}
details.html(团队详细信息)


{{team}}
昵称:{{昵称[0]}

经理:{{经理[0]}

体育场:{{体育场[0]}

容量:{{容量[0]}


将索引传递给
团队
路线。这是最终的解决方案

football.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/')
def index():
  return render_template('football.html')

@app.route('/bpl/')
def bplLink():
  teamName = ['AFC Bournemouth', 'Arsenal', 'Aston Villa', 'Chelsea',
  'Crystal Palace', 'Everton', 'Leicester City', 'Liverpool', 'Manchester City',
  'Manchester United', 'Newcastle United', 'Norwich City', 'Southampton',
  'Stoke City', 'Sunderland', 'Swansea City', 'Tottenham Hotspur',
  'Watford', 'West Bromwich Albion', 'West Ham United']
  return render_template('bplteams.html', teamName=teamName)

@app.route('/bpl/<team>')
def team(team):
  index = int(request.args.get('index', 0))
  stadium = ['Dean Court', 'Emirates Stadium', 'Villa Park', 'Stamford Bridge',
  'Selhurst Park', 'Goodison Park', 'King Power Stadium', 'Anfield',
  'Etihad Stadium', 'Old Trafford', 'St James Park', 'Carrow Road',
  'St Marys Stadium', 'Britannia Stadium', 'Stadium of Light',
  'Liberty Stadium', 'White Hart Lane', 'Vicarage Road', 'The Hawthorns',
  'Boleyn Ground']
  badge = ['bournemouth.png', 'arsenal.png', 'aston-villa.png', 'chelsea.png',
  'crystal-palace.png', 'everton.png', 'leicester.png', 'liverpool',
  'man-city.png', 'man-united.png', 'newcastle.png', 'norwich.png',
  'southampton.png', 'stoke.png', 'sunderland.png', 'swansea.png',
  'tottenham.png', 'watford.png', 'west-brom.png', 'west-ham.png']
  capacity = ['11,464', '60,260', '42,660', '41,798', '25,073', '39,571',
  '32,312', '44,742', '55,097', '75,653', '52,338', '27,010', '32,505',
  '27,740', '48,707', '20,909', '36,284', '21,500', '26,850', '35,345']
  manager = ['Eddie Howe', 'Arsene Wenger', 'Kevin MacDonald (caretaker)',
  'Jose Mourinho', 'Alan Pardew', 'Roberto Martinez', 'Claudio Ranieri',
  'Jurgen Klopp', 'Manuel Pellegrini', 'Louis van Gaal', 'Steve McClaren',
  'Alex Neil', 'Ronald Koeman', 'Mark Hughes', 'Sam Allardyce', 'Garry Monk',
  'Mauricio Pochettino', 'Quique Flores', 'Tony Pulis', 'Slaven Bilic']
  nickname = ['Cherries', 'Gunners', 'Villans', 'Blues', 'Eagles', 'Toffees',
  'Foxes', 'Reds', 'City', 'Red Devils', 'Magpies', 'Canaries', 'Saints',
  'Potters', 'Black Cats', 'Swans', 'Spurs', 'Hornets', 'Baggies', 'Irons']
  return render_template('details.html', team=team, stadium=stadium,
                         badge=badge, capacity=capacity, manager=manager,
                         nickname=nickname, index=index)
details.html

<!doctype html>
<body>
<h1>{{ team }}</h1>
<img src="/static/{{ badge[index] }}">
<p>Nickname: {{ nickname[index] }}</p>
<p>Manager: {{ manager[index] }}</p>
<p>Stadium: {{ stadium[index] }}</p>
<p>Capacity: {{ capacity[index] }}</p>
</body>
</html>

{{team}}
昵称:{{昵称[索引]}

经理:{{Manager[index]}

体育场:{{体育场[索引]}

容量:{{Capacity[index]}


也许您应该在
团队
功能中使用数组索引?您的意思是像我处理其他列表一样吗?对不起,如果这是愚蠢的,我是python烧瓶的初学者!否,您应该从
team
函数返回正确的值,例如
stadium[team]
manager[team]
或同时返回所有值,或者返回一个复杂的对象。因此,我是否希望{'name':'AFC Bournemouth','badge':'Bournemouth.png','stadium':'Dean Court','capacity':'11464','manager':'Eddie Howe','昵称':'Cherries',等等?我已经解决了,非常感谢您的帮助!非常感谢!
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/')
def index():
  return render_template('football.html')

@app.route('/bpl/')
def bplLink():
  teamName = ['AFC Bournemouth', 'Arsenal', 'Aston Villa', 'Chelsea',
  'Crystal Palace', 'Everton', 'Leicester City', 'Liverpool', 'Manchester City',
  'Manchester United', 'Newcastle United', 'Norwich City', 'Southampton',
  'Stoke City', 'Sunderland', 'Swansea City', 'Tottenham Hotspur',
  'Watford', 'West Bromwich Albion', 'West Ham United']
  return render_template('bplteams.html', teamName=teamName)

@app.route('/bpl/<team>')
def team(team):
  index = int(request.args.get('index', 0))
  stadium = ['Dean Court', 'Emirates Stadium', 'Villa Park', 'Stamford Bridge',
  'Selhurst Park', 'Goodison Park', 'King Power Stadium', 'Anfield',
  'Etihad Stadium', 'Old Trafford', 'St James Park', 'Carrow Road',
  'St Marys Stadium', 'Britannia Stadium', 'Stadium of Light',
  'Liberty Stadium', 'White Hart Lane', 'Vicarage Road', 'The Hawthorns',
  'Boleyn Ground']
  badge = ['bournemouth.png', 'arsenal.png', 'aston-villa.png', 'chelsea.png',
  'crystal-palace.png', 'everton.png', 'leicester.png', 'liverpool',
  'man-city.png', 'man-united.png', 'newcastle.png', 'norwich.png',
  'southampton.png', 'stoke.png', 'sunderland.png', 'swansea.png',
  'tottenham.png', 'watford.png', 'west-brom.png', 'west-ham.png']
  capacity = ['11,464', '60,260', '42,660', '41,798', '25,073', '39,571',
  '32,312', '44,742', '55,097', '75,653', '52,338', '27,010', '32,505',
  '27,740', '48,707', '20,909', '36,284', '21,500', '26,850', '35,345']
  manager = ['Eddie Howe', 'Arsene Wenger', 'Kevin MacDonald (caretaker)',
  'Jose Mourinho', 'Alan Pardew', 'Roberto Martinez', 'Claudio Ranieri',
  'Jurgen Klopp', 'Manuel Pellegrini', 'Louis van Gaal', 'Steve McClaren',
  'Alex Neil', 'Ronald Koeman', 'Mark Hughes', 'Sam Allardyce', 'Garry Monk',
  'Mauricio Pochettino', 'Quique Flores', 'Tony Pulis', 'Slaven Bilic']
  nickname = ['Cherries', 'Gunners', 'Villans', 'Blues', 'Eagles', 'Toffees',
  'Foxes', 'Reds', 'City', 'Red Devils', 'Magpies', 'Canaries', 'Saints',
  'Potters', 'Black Cats', 'Swans', 'Spurs', 'Hornets', 'Baggies', 'Irons']
  return render_template('details.html', team=team, stadium=stadium,
                         badge=badge, capacity=capacity, manager=manager,
                         nickname=nickname, index=index)
<!doctype html>
<body>
{% for team in teamName %}
    <a href="{{ url_for('team', team=team, index=loop.index0) }}">{{team}}</a><br />
{% endfor %}
</body>
</html>
<!doctype html>
<body>
<h1>{{ team }}</h1>
<img src="/static/{{ badge[index] }}">
<p>Nickname: {{ nickname[index] }}</p>
<p>Manager: {{ manager[index] }}</p>
<p>Stadium: {{ stadium[index] }}</p>
<p>Capacity: {{ capacity[index] }}</p>
</body>
</html>