用flask做个克苏鲁普及网站

框架:

先创建好这些目录:
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




    
    
    
    Document


    

克总发糖

书名

作者:{{author}}
书名:{{book}}
下载:链接

完成的效果:

在手机上看:


用flask做个克苏鲁普及网站_第1张图片
图片发自App

你可能感兴趣的:(用flask做个克苏鲁普及网站)