分享

flask第二十九篇——一个例子+【更新内容通知】

 孟船长 2022-02-24

大家先自己写一下,船长写这个花了半个小时,因为我和大家一样,也是新手:

写一个页面如下,点击书名以后跳转到书的详情页

书的信息如下:

books = [
    {
        'id': 1,
        'title': u'三国演义',
        'author': u'罗贯中',
        'profile': u'《三国演义》是中国古典四大名著之一,是中国第一部长篇章回体历史演义小说,全名为《三国志通俗演义》(又称《三国志演义》),作者是元末明初的著名小说家罗贯中。',
        'price': u'100'
    },
    {
        'id': 2,
        'title': u'水浒传',
        'author': u'施耐庵',
        'profile': u'《水浒传》,是中国四大名著之一,全书描写北宋末年以宋江为首的108位好汉在梁山聚义,以及聚义之后接受招安、四处征战的故事。',
        'price': u'90'
    },
    {
        'id': 3,
        'title': u'西游记',
        'author': u'吴承恩',
        'profile': u'《西游记》为明代小说家吴承恩所著。取材于《大唐西域记》和民间传说、元杂剧。宋代《大唐三藏取经诗话》(本名《大唐三藏取经记》)是西游记故事见于说话文字的最早雏形,其中,唐僧就是以玄奘法师为原型的。',
        'price': u'120'
    },
    {
        'id': 4,
        'title': u'红楼梦',
        'author': u'曹雪芹',
        'profile': u'《红楼梦》,中国古典四大名著之首,清代作家曹雪芹创作的章回体长篇小说[1]  ,又名《石头记》《金玉缘》。此书分为120回“程本”和80回“脂本”两种版本系统。新版通行本前80回据脂本汇校,后40回据程本汇校,署名“曹雪芹著,无名氏续,程伟元、高鹗整理”[2]  。后40回作者尚有争议,但是对于矮化甚至腰斩后40回的极端倾向也应保持警惕。',
        'price': u'110'
    }
]

大家先自己写写试试,再看答案。

答案:

demo.py

# coding: utf-8

from flask import Flask,render_template app = Flask(__name__) app.debug = True

books = [    {
       'id': 1,
       'title': u'三国演义',        
       'author': u'罗贯中',        
       'profile': u'《三国演义》是中国古典四大名著之一,是中国第一部长篇章回体历史演义小说,全名为《三国志通俗演义》(又称《三国志演义》),作者是元末明初的著名小说家罗贯中。',        
       'price': u'100'    },    {
       'id': 2,        
       'title': u'水浒传',        
       'author': u'施耐庵',        
       'profile': u'《水浒传》,是中国四大名著之一,全书描写北宋末年以宋江为首的108位好汉在梁山聚义,以及聚义之后接受招安、四处征战的故事。',        
       'price': u'90'    },    {
       'id': 3,        
       'title': u'西游记',        
       'author': u'吴承恩',        
       'profile': u'《西游记》为明代小说家吴承恩所著。取材于《大唐西域记》和民间传说、元杂剧。宋代《大唐三藏取经诗话》(本名《大唐三藏取经记》)是西游记故事见于说话文字的最早雏形,其中,唐僧就是以玄奘法师为原型的。',        
       'price': u'120'    },    {
       'id': 4,        
       'title': u'红楼梦',        
       'author': u'曹雪芹',        
       'profile': u'《红楼梦》,中国古典四大名著之首,清代作家曹雪芹创作的章回体长篇小说[1]  ,又名《石头记》《金玉缘》。此书分为120回“程本”和80回“脂本”两种版本系统。新版通行本前80回据脂本汇校,后40回据程本汇校,署名“曹雪芹著,无名氏续,程伟元、高鹗整理”[2]  。后40回作者尚有争议,但是对于矮化甚至腰斩后40回的极端倾向也应保持警惕。',        
       'price': u'110'    } ]


@app.route('/')
def hello_world():    return render_template('index.html', books=books)


@app.route('/detail/<id>/')
def book_detail(id):    id = int(id)-1    return render_template('bookcontent.html', books=books, i=id)

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

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>四大名著</title>
</head>
<style>
    #table_test tr{
        width: 50px;
    }
    #table_test tr td{
        width: 70px;
    }
</style>
<body>
    <table border="1" id="table_test">
        <thead>
            <tr bgcolor="#f0f8ff">
                <td>id</td>
                <td>书名</td>
                <td>作者</td>
                <td>价格</td>
            </tr>
        </thead>
        <tbody>
            {% for book in books %}
                <tr>
                    <td>{{ book.id }}</td>
                    <td><a href="{{ url_for('book_detail', id=book.id) }}">{{ book.title }}</a></td>
                    <td>{{ book.author }}</td>
                    <td>{{ book.price }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

bookcontent.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>书籍详情</title>
</head>
<body>
    <p>{{ books[i].profile }}</p>
</body>
</html>

内容更新通知

        因为web开发不可避免的要自己写页面,所以没办法,不讲HTML大家包括我根本没法继续下去,所以船长决定发10片文章,讲完前端基础课。每节课信息量可能有些大,我想愿意学的自然能坚持下来。为了早点拿到高工资,学呗~~希望各位和我一起加油~

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多