redirect:重定向,會改變urlcss
render_template:模板渲染,用模板來渲染當前頁,不會改變urlhtml
【描述】前端
博客項目,在login請求中設置g對象,在register請求中取得g對象,取得Noneajax
@bp.route('/login',methods=('GET','POST')) def login(): #... g.myname = 'john' @bp.route('/register',methods=('GET','POST')) def register(): #... name = g.get('myname') print(name) # None
【分析】sql
文檔中說道:To share data that is valid for one request only from one function to another ...shell
注意是一次請求,一次!數據庫
要實現跨request須要用到sessionflask
【描述】windows
最終須要的效果圖:後端
個人數據庫:
CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password TEXT NOT NULL ); CREATE TABLE post ( id INTEGER PRIMARY KEY AUTOINCREMENT, author_id INTEGER NOT NULL, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, title TEXT NOT NULL, body TEXT NOT NULL, FOREIGN KEY (author_id) REFERENCES user (id) );
【解決方案1】
1.寫sql的時候這樣寫 :SUBSTR(p.body,1,100) as body
SELECT p.id , title, SUBSTR(body,1,100) as body,created,author_id,username FROM post p JOIN user u ON p.author_id = u.id ORDER BY created DESC
注意必定要定義別名 ... as body,否則會致使在html模板中取不到值
2.前端css代碼
font-size:0.6em;
overflow:hidden;
white-space: nowrap;
text-overflow:ellipsis;
【解決方案2】
在模板中使用過濾器(truncatechars),以下將截斷100位以後的字符串,並以...表示
<p class="list-group-item-text blog-index-textbody">{{ post['body']|truncatechars:100 }}</p>
【描述】
【解決方案】
看了一下VScode的TERMINAL是powershell,CMD 和 powershell 的命令不同
powershell
> $env:FLASK_APP = "myapp"
> $env:FLASK_ENV="development"
> flask run
CMD
> set FLASK_APP=myapp > set FLASK_ENV=development > flask run
【備註】
1.由於導出的環境變量問題,致使windows下初始化db失敗(flask init-db),經過上述方案成功解決
2.windows下把這啓動flask服務的命令寫進bat文件,之後每次只須要運行這個bat文件就能夠了,固然命令要用CMD的那個
運行方法: ./start.bat
【描述】
博客表:
CREATE TABLE post ( id INTEGER PRIMARY KEY AUTOINCREMENT, author_id INTEGER NOT NULL, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, title TEXT NOT NULL, body TEXT NOT NULL, FOREIGN KEY (author_id) REFERENCES user (id) );
其中created字段時間比實際時間早了8小時左右
【解決方案】
網上查了一下是由於時區的關係
記得datetime外層必定要裹上括號
created TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime')),
或者
created TIMESTAMP NOT NULL DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')),
參考:https://www.cnblogs.com/GDLMO/archive/2010/07/19/1780920.html
緣由是我用了sqlite manager打開了數據庫觀察,致使刪除文章失敗。。。嘔
【描述】
模板: <p class="list-group-item-text blog-index-textbody">{{ post['body'] }}</p>
顯示以下:
【解決方案】
正則去掉標籤
估計是ajax
【解決方案】redirect
【解決方案】ajax
【解決方案】
用隱藏輸入框<input type="hidden" name="form1"/>
【方法一】
利用外鍵完整性約束,在外鍵加入ON DELETE CASCADE
DROP TABLE IF EXISTS user; DROP TABLE IF EXISTS post; DROP TABLE IF EXISTS comment; CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password TEXT NOT NULL ); CREATE TABLE post ( id INTEGER PRIMARY KEY AUTOINCREMENT, author_id INTEGER NOT NULL, created TIMESTAMP NOT NULL DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')), title TEXT NOT NULL, body TEXT NOT NULL, FOREIGN KEY (author_id) REFERENCES user (id) ); CREATE TABLE comment ( authorid INTEGER NOT NULL, postid INTEGER NOT NULL, userid INTEGER NOT NULL, ctext TEXT NOT NULL, ctime TIMESTAMP NOT NULL DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')), enable_dis BOOLEAN NOT NULL, reply_targetid INTEGER, FOREIGN KEY (userid) REFERENCES user (id), FOREIGN KEY (reply_targetid) REFERENCES user (id), FOREIGN KEY (authorid) REFERENCES post (author_id), FOREIGN KEY (postid) REFERENCES post (id) ON DELETE CASCADE );
注意:sqlite須要手動開啓外鍵完整性約束: sqlite> PRAGMA foreign_keys = ON;
參考:https://blog.csdn.net/qq_34082034/article/details/54927680
【描述】
一個簡單的路由:
@app.route('/hello') def hello(): pass return render_template('index.html')
前端傳來一個form想由後端處理,只須要邏輯處理後update一下數據庫就行,不用返回新頁面,如何實現呢?
【解決方案】ajax
【描述】
flask會檢查{{...}}裏面的內容,而無論在模板中是否被註釋。由於路由有所改動,致使報錯。。
【解決方案】
刪掉,原理請參考源碼
在block中加入"-"符號。例如:------{%- if test -%}{%- endif -%}