from flask import Blueprint, render_template, request, url_for, flash, redirect from database import get_owp_db import os import sqlite3 def get_owp_db_conn(): conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/sqlite/owp.db') conn.row_factory = sqlite3.Row return conn blog_bp = Blueprint('blog', __name__) @blog_bp.route('/') def home(): db = get_owp_db() conn = get_owp_db_conn() sql_logs = "SELECT * from logs;" sql_posts = "SELECT * from posts;" logs = conn.execute(sql_logs).fetchall() posts = conn.execute(sql_posts).fetchall() conn.close() return render_template('home.html', logs=logs[::-1], posts=posts[::-1]) @blog_bp.route('/about/') def about(): return render_template('about.html') pass @blog_bp.route('/posts/') def posts_list(): conn = get_owp_db_conn() sql_posts = "SELECT * from posts;" posts = conn.execute(sql_posts).fetchall() conn.close() return render_template('list.html', posts=posts[::-1]) @blog_bp.route('/posts//') def show_posts_id(posts_id): conn = get_owp_db_conn() sql_posts = "SELECT * FROM posts WHERE id = ?" posts = conn.execute(sql_posts, (posts_id,)).fetchone() conn.close() return render_template('posts.html', posts=posts) # 数据库测试 @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)}"