框架:
先创建好这些目录:
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>
完成的效果:
在手机上看: