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



def getDB():
    '''连接数据库'''
    db = pymysql.connect(host='localhost',user='root',password='shengji.',database='douban',)
    return db





@app.route('/')
def home():  # put application's code here
    return render_template('index.html')


@app.route('/movies')
@app.route('/movies/<int:page>')
def movies(page = 1):# put application's code here
    db = getDB()
    cursor = db.cursor()
    #查询当前页面电影列表
    sql = "select ranks,links,film_name,film_name_en,rating_num,rating_people,summary,director from movies limit {},{} ".format((page-1)*25,page*25)
    cursor.execute(sql)
    data = cursor.fetchall()
    datalist = []
    for item in data:
        datalist.append(item)
    #查询电影总数
    sql1 = "select count(*) from movies"
    cursor.execute(sql1)
    total = cursor.fetchone()

    cursor.close()
    db.close()
    return render_template("movies.html",page=page,movies = datalist,countnum = int(int(total[0])/25))



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