from flask import Flask,render_template,views,request
app = Flask(__name__)
movies = [{
'src':'static/images/1.jpg',
'title':'泳者之心',
'score':9.0
},{
'src':'static/images/2.jpg',
'title':'玛克辛',
'score':5.9
},{
'src':'static/images/3.jpg',
'title':'阿诺啦',
'score':6.9
},{
'src':'static/images/4.jpg',
'title':'神偷奶爸4',
'score':6.2
},{
'src':'static/images/5.jpg',
'title':'来福大酒店',
'score':7.0
}]
tvs = [{
'src':'static/images/6.jpg',
'title':'仙逆 年番',
'score':9.1
},{
'src':'static/images/7.jpg',
'title':'窥探',
'score':8.7
},{
'src':'static/images/8.jpg',
'title':'神烦警探 第八季',
'score':9.5
},{
'src':'static/images/9.jpg',
'title':'大秦帝国之裂变',
'score':9.3
},{
'src':'static/images/10.jpg',
'title':'将死之人',
'score':7.1
}]
@app.route('/')
def index():
context = {
'movies':movies,
'tvs':tvs
}
return render_template('index.html', **context)
def list_view():
data = {}
id_val = int(request.args.get('sid'))
if id_val == 1:
data = {
'items':movies,
'sid': id_val
}
else:
data = {
'items': tvs,
'sid':id_val
}
return render_template('list.html',**data)
app.add_url_rule('/list',view_func=list_view)
if __name__ == '__main__':
app.run(debug=True,port=5001)
marco.html
{% macro listModel(src,title,score) %}
<div class="item-group">
<img src="{{ src }}" alt="" class="thumbnail">
<div class="item-title">{{title}}</div>
<div class="item-rating">
{% set lights = (score/2)|int %}
{% set half = (score%2)|int %}
{% set gray = 5-lights-half %}
{% for l in range(0,lights) %}
<img src="{{ url_for('static',filename='images/rate_light.png') }}" alt="">
{% endfor %}
{% for h in range(0,half) %}
<img src="{{ url_for('static',filename='images/rate_half.jpg') }}" alt="">
{% endfor %}
{% for g in range(0,gray) %}
<img src="{{ url_for('static',filename='images/rate_gray.png') }}" alt="">
{% endfor %}
{{ score }}
</div>
</div>
{% endmacro %}
{% macro listGroup(title,items,sid)%}
<div class="item-list-group">
<div class="item-list-top">
<span class="module-title">{{title}}</span>
<a href="{{ url_for('list_view',sid=sid) }}" class="more-btn">更多</a>
</div>
<div class="list-group">
{% for m in items[0:3] %}
{{listModel(m.src,m.title,m.score)}}
{% endfor %}
</div>
</div>
{% endmacro %}
base.html
{% from 'macro.html' import listGroup,listModel %}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{% block title%}
title
{% endblock %}</title>
<link rel="stylesheet" href="{{ url_for('static',filename='css/base.css') }}">
<link rel="stylesheet" href="{{ url_for('static',filename='css/item.css') }}">
</head>
<body>
<div class="container">
<div class="search-group"><input type="text" class="search-input"></div>
{% block content%}
{% endblock %}
</div>
</body>
</html>
index.html
{% extends 'base.html' %}
{% block content%}
{{ listGroup('电影', movies, 1) }}
{{ listGroup('电视', tvs, 2) }}
{% endblock %}
list.html
{% extends 'base.html' %}
{% block title%}
{{sid}}
{% endblock %}
{% block content%}
{% for m in items %}
{{listModel(m.src,m.title,m.score)}}
{% endfor %}
{% endblock %}
extends
标签用于模板继承,搭配block使用
macro 可以将一组模板标签封装成一个宏,然后在模板的其他地方或不同的模板中调用它