我试图创建一个烧瓶项目,根据您选择的团队显示某些细节。例如,如果我点击榜单上第四支球队的切尔西,我希望第四支徽章,经理,球场,容量和昵称出现,但我不知道该怎么做。
(目前我只是输出每个列表中的第一个内容,只是为了显示在屏幕上的内容)
football.py
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)
bplteams.html(列出所有团队)
<!doctype html>
<body>
{% for team in teamName %}
<a href="/bpl/{{team}}">{{team}}</a><br />
{% endfor %}
</body>
</html>
details.html(团队详情)
<!doctype html>
<body>
<h1>{{ team }}</h1>
<img src="/static/{{ badge[0] }}">
<p>Nickname: {{ nickname[0] }}</p>
<p>Manager: {{ manager[0] }}</p>
<p>Stadium: {{ stadium[0] }}</p>
<p>Capacity: {{ capacity[0] }}</p>
</body>
</html>
答案 0 :(得分:0)
将索引传递给team
路由。这是最终的解决方案。
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)
bplteams.html
<!doctype html>
<body>
{% for team in teamName %}
<a href="{{ url_for('team', team=team, index=loop.index0) }}">{{team}}</a><br />
{% endfor %}
</body>
</html>
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>