python3 flask开发 19 url_for根据参数(传参)动态加载显示文件

index.html(templates文件夹下)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{{ name }}'s Watchlist</title>
    <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
</head>
<body>
    
<h2>
    {{ name }}'s Watchlist
</h2>
    <p>{{ movies|length }} Titles</p>
    <ul class="movie-list">
        {% for movie in movies %} 
        <li>{{ movie.title }} - {{ movie.year }}<a href="{{ url_for('static', filename=movie.title) }}">111</a></li>  
        {% endfor %} 
    </ul>
    <footer>
        <small>&copy; 2018 <a href="http://helloflask.com/tutorial">HelloFlask</a></small>
	</footer>
    <img alt="Walking Totoro" class="totoro" src="{{ url_for('static', filename='images/totoro.gif')}}">
</body>
</html>

from flask import Flask, render_template
import time
import os

app = Flask(__name__)
    
movies = [
    {'title': 'My Neighbor Totoro', 'year': '1988'},
    {'title': 'Dead Poets Society', 'year': '1989'},
    {'title': 'A Perfect World', 'year': '1993'},
    {'title': 'Leon', 'year': '1994'},
    {'title': 'Mahjong', 'year': '1996'},
    {'title': 'Swallowtail Butterfly', 'year': '1996'},
    {'title': 'King of Comedy', 'year': '1999'},
    {'title': 'Devils on the Doorstep', 'year': '1999'},
    {'title': 'WALL-E', 'year': '2008'},
    {'title': 'The Pork of Music', 'year': '2012'},
]

fileDict={'title': 'The Pork of Music', 'year': '2012'}
fileList=[]
fileList.append(fileDict)
print(fileList)
num=0

path = "F:\\PythonWebPYCode\\mma"
listS = os.listdir(path)
print(listS)
print("*************************")

for i in listS:
    #print(i)      os.path.join(path, name)
    #i = os.path.join(path, i)
    fileDict['title']=i
    fileDict['year']=time.ctime(os.path.getatime(i))
    print(fileDict)
    fileList.append(fileDict.copy())
    #fileList[num]=fileList[num]+fileD
    

print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&")    
print(fileList)

@app.route('/')
def index():
    return render_template('index.html', name="xxxd", movies=fileList)


if __name__ == '__main__':
    app.run()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值