跟着麦叔学flask,感谢b站麦叔
This commit is contained in:
29
blueprint/blog_views.py
Executable file
29
blueprint/blog_views.py
Executable file
@@ -0,0 +1,29 @@
|
||||
from flask import Blueprint, render_template
|
||||
from database import get_owp_db
|
||||
import os
|
||||
|
||||
|
||||
blog_bp = Blueprint('blog', __name__)
|
||||
|
||||
@blog_bp.route('/')
|
||||
def home():
|
||||
db = get_owp_db()
|
||||
return render_template('home.html')
|
||||
|
||||
@blog_bp.route('/about/')
|
||||
def about():
|
||||
return render_template('about.html')
|
||||
pass
|
||||
|
||||
@blog_bp.route('/db_test/')
|
||||
def test_db():
|
||||
db = get_owp_db()
|
||||
|
||||
try:
|
||||
cur = db.execute('SELECT sqlite_version();')
|
||||
ver = cur.fetchone()[0]
|
||||
return f"数据库连接成功!SQLite 版本是: {ver}"
|
||||
except Exception as e:
|
||||
return f"连接失败: {str(e)}"
|
||||
|
||||
|
||||
95
blueprint/study_views.py
Executable file
95
blueprint/study_views.py
Executable file
@@ -0,0 +1,95 @@
|
||||
from flask import Blueprint, render_template, request, url_for, flash, redirect
|
||||
import os
|
||||
import sqlite3
|
||||
|
||||
study_bp = Blueprint('study', __name__)
|
||||
|
||||
def get_db_conn():
|
||||
conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/database.db')
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
def get_posts(posts_id):
|
||||
conn = get_db_conn()
|
||||
posts = conn.execute("SELECT *, datetime(created, '+8 hours') AS created_8 FROM posts WHERE id = ?", (posts_id,)).fetchone()
|
||||
conn.close()
|
||||
return posts
|
||||
|
||||
@study_bp.route('/')
|
||||
def home():
|
||||
conn = get_db_conn()
|
||||
sql = "SELECT *, datetime(created, '+8 hours') AS created_8 FROM posts;"
|
||||
posts = conn.execute(sql).fetchall()
|
||||
conn.close()
|
||||
return render_template('study/home.html', posts=posts[::-1])
|
||||
|
||||
@study_bp.route('/about/')
|
||||
def about():
|
||||
return render_template('study/about.html')
|
||||
|
||||
@study_bp.route('/posts/<int:posts_id>')
|
||||
def show_posts_id(posts_id):
|
||||
conn = get_db_conn()
|
||||
posts = get_posts(posts_id)
|
||||
if posts is None:
|
||||
return '抱歉,未查到该文章'
|
||||
|
||||
return render_template('study/posts.html', posts=posts)
|
||||
|
||||
@study_bp.route('/posts/new/', methods=('GET', 'POST'))
|
||||
def new():
|
||||
if request.method == "POST":
|
||||
title = request.form['title']
|
||||
content = request.form['content']
|
||||
|
||||
if not title:
|
||||
flash('标题不能为空')
|
||||
elif not content:
|
||||
flash('内容不能为空')
|
||||
else:
|
||||
conn = get_db_conn()
|
||||
conn.execute('insert into posts (title, content) values (?, ?)', (title, content))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
flash('文章发布成功')
|
||||
return redirect(url_for('study.home'))
|
||||
|
||||
return render_template('study/new.html')
|
||||
|
||||
@study_bp.route('/posts/<int:posts_id>/edit', methods=('GET', 'POST'))
|
||||
def edit_posts_id(posts_id):
|
||||
if request.method == "POST":
|
||||
title = request.form['title']
|
||||
content = request.form['content']
|
||||
|
||||
if not title:
|
||||
flash('标题不能为空')
|
||||
elif not content:
|
||||
flash('内容不能为空')
|
||||
else:
|
||||
conn = get_db_conn()
|
||||
conn.execute('update posts set title= ?, content = ? where id = ?', (title, content, posts_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return redirect(url_for('study.home'))
|
||||
|
||||
return render_template('study/edit.html', posts=posts)
|
||||
|
||||
@study_bp.route('/posts/<int:posts_id>/delete/', methods=['POST' , 'GET'])
|
||||
def delete_posts_id(posts_id):
|
||||
if request.method == "POST":
|
||||
posts = get_posts(posts_id)
|
||||
conn = get_db_conn()
|
||||
conn.execute('DELETE FROM posts WHERE id = ?', (posts_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
flash('删除成功')
|
||||
return redirect(url_for('study.home'))
|
||||
|
||||
elif request.method == "GET":
|
||||
posts = get_posts(posts_id)
|
||||
conn = get_db_conn()
|
||||
conn.execute('DELETE FROM posts WHERE id = ?', (posts_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return render_template('study/delete.html', posts=posts)
|
||||
Reference in New Issue
Block a user