美文网首页我爱编程工具癖
用flask做个克苏鲁普及网站

用flask做个克苏鲁普及网站

作者: 圣_狒司机 | 来源:发表于2018-08-01 21:24 被阅读43次

框架:

先创建好这些目录:
falsk的套路就是网站框架目录基本相同,你可以自己做个bat文件一键生成这些目录;

C:.
│  books.db
│  list.txt
│  main.py
│
├─app
│  │  app.py
│  │  models.py
│  │  __init__.py
│  │
│  ├─imgs
│  │      0.jpg
│  │
│  ├─statics
│  ├─templates
│  │      books.html
│
└─test

逐个文件的代码:

main.py

from app.app import app

if __name__== "__main__":
    app.run(debug=True,host='0.0.0.0')  

models.py

import sqlite3

class db():
    def __init__(self):
        self.conn = sqlite3.connect('books.db',check_same_thread=False)
        self.cur = self.conn.cursor()
        self.create_db()
        
    def create_db(self):
        self.cur.execute('drop table if exists books')
        self.cur.execute('create table books (id integer primary key autoincrement,author text,book text)')
        with open('list.txt') as f:
            context = f.readlines()
        for i in context:
            data = ' '.join(i.split()[5:-1]),i.split()[3]
            self.cur.execute('insert into books (author , book) values (?,?)',data )
        test = self.cur.execute('select * from books;').fetchall()
        print(test)
        self.conn.commit()

    def insert(self,data):
        self.cur.execute('insert into books (author,book) values (?,?)',(data[0],data[1]))
        self.conn.commit()
    
    def select(self,book):
        self.cur.execute('select * from books where book = ?;',(book,))
        content = self.cur.fetchall()
        return content

app.py

from flask import Flask,render_template,request
from app.models import db

app = Flask(__name__)
models = db()

@app.route('/',methods=["GET","POST"])
def index():
    if request.method == "POST":
        book = request.form.get('book')
        result = models.select(book)
        if result is not None:
            try:

                id,author,book = result[0]
                download = r'C:\Users\super\Documents\book\原著译文' + book 
                return render_template('books.html',author=author,book=book,download=download)
            except:pass

    return render_template('books.html')  

books.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body background="../imgs/0.jpg">
    <h1>克总发糖</h1>
    <form action="" method="post">
        书名<input type="text" name="book" ><br>
        <input type="submit" >
    </form>
    <hr>
    作者:{{author}}<br>
    书名:{{book}}<br>
    下载:<a href="{{download}}">链接</a>
    
</body>
</html>

完成的效果:

在手机上看:


用flask做个克苏鲁普及网站

相关文章

  • 用flask做个克苏鲁普及网站

    框架: 先创建好这些目录:falsk的套路就是网站框架目录基本相同,你可以自己做个bat文件一键生成这些目录; 逐...

  • 新年的古神菜单,未完待续……

    古神菜单 一,克苏鲁之拥 材料:克苏鲁之触(由于获取克苏鲁之触会造成世界毁灭,建议用鱿鱼的触手替代)、克苏鲁之翼(...

  • 克苏鲁

    克苏鲁是美国小说家霍华德·菲利普·洛夫克拉夫特所创造的克苏鲁神话中的一个邪恶存在。在奥古斯特·威廉·德雷斯为克苏鲁...

  • 克苏鲁

    众神遗忘之地,便是克苏鲁 (一) 我在哪,这里是,宇宙中心吗?我看到星系缓慢朝我环绕。 什么时间,是宇宙的初始或是...

  • 克苏鲁

    苏醒吧,克苏鲁! 尽情拍打如山的翅膀 伸展绵延千里的触须 低头的人们 一定会慑于你的英姿 圣火、玉液 万物都将在燃...

  • 克苏鲁笔记-1

    作品: 克苏鲁的呼唤 洛夫克拉夫特 新奥尔良的克苏鲁信徒在这部作品中被展现。他们在沼泽密林中举行仪式祭拜克苏鲁。 ...

  • 说明

    此文集用于克苏鲁风格文章的尝试,一种将克苏鲁本土化的尝试。

  • 扶苏奔鲁(1)

    浏览克苏鲁板块有人推荐这篇文,看了下挺有意思,与君共赏。 ps:鲁不会就是克苏鲁吧? -----------以下正...

  • 那些最接近于现实神话的造物,克苏鲁,就在我们身边

    克苏鲁邪神,美国作家洛夫克拉夫特所创造的一个诡秘世界,即便用现在的科学来解释也仍然令人毛骨悚然。 克苏鲁神话的核心...

  • 克苏鲁神话(一)克苏鲁之父

    克苏鲁这个词,近年来在各种影视作品、文学作品、电子游戏中频频出现,成为了一个有些热度的话题。 不过,一定有人好奇,...

网友评论

    本文标题:用flask做个克苏鲁普及网站

    本文链接:https://www.haomeiwen.com/subject/yecdvftx.html