代码片段分析:
from flask import *
from flask import make_response,render_template,request,session
from contextlib import closing
import sqlite3
import os
basedir=os.path.abspath(os.path.dirname(__file__))
path=os.path.join(basedir,'data.db')
#app配置文件
DATABASE='data.db'
DEBUG=True
SECRET_KEY='I am sunqi'
USERNAME='sunqi'
PASSWORD='213919'
app=Flask(__name__)
app.config.from_object(__name__) #从当前文件自动寻找参数导入app配置
def connect():
return sqlite3.connect(app.config['DATABASE'])
def init_db():
with closing(connect()) as db:
with app.open_resource('schema.sql') as f:
db.cursor().executescript(f.read().decode())
db.commit()
@app.before_request
def before():
g.db=connect()
@app.teardown_request
def tear(exception):
g.db.close()
@app.template_filter("md") 相当于字典告诉网页需要处理
# 过滤器将html中的md文档转义成html格式的内容在网页中显示
网页中使用 {{var(md文档字符串)|md|safe}} safe标记该内容段安全
def markdown_to_html(txt):
from markdown import markdown
return markdown(txt)
schema.sql文件内容:
drop table if exists entries;
create table entries (
id integer primary key autoincrement,
title string not null,
text string not null
);
学了几天语法倒是懂了,关键内部怎么实现的还是似懂非懂 0.0