一个简单的博客项目,flask框架,数据库为sqlite,增删改已经写出来了,查毫无头绪(希望在pageForClient页加上搜索框,根据文章题目搜索),请给些思路
#这是baseForClient
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="{{ url_for('static', filename= 'css/bootstrap.min.css') }}">
<title>{% block title %} {% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-md navbar-light bg-light">
<a class="navbar-brand" href="{{ url_for('pageForClient')}}">博客</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
</ul>
</div>
</nav>
<div class="container">
{% for message in get_flashed_messages() %}
<div class="alert alert-danger">{{ message }}</div>
{% endfor %}
{% block content %} {% endblock %}
</div>
<script src="{{url_for('static', filename='js/jquery.slim.min.js')}}" ></script>
<script src="{{url_for('static', filename='js/popper.min.js')}}" ></script>
<script src="{{url_for('static', filename='js/bootstrap.min.js')}}" ></script>
</body>
</html>
#这是pageForClient
{% extends 'baseForClient.html' %}
{% block content %}
<h1>{% block title %} 浏览博客(游客页面){% endblock %}</h1></br>
<form action="#" ><input name="word" type="search" />
<input type="submit" value="查找" /></form>
{% for clientpost in clientposts %}
<a href="{{ url_for('clientpost', clientpost_id=clientpost['id']) }}">
<h2>{{ clientpost['title'] }}</h2>
</a>
<span class="badge badge-primary">{{ clientpost ['created'] }}</span>
<hr>
<hr>
{% endfor %}
{% endblock %}
#这是blog.py
from flask import Flask, render_template, request, url_for, flash, redirect
import sqlite3
app = Flask(__name__)
app.config['SECRET_KEY'] = 'i am fat again'
def get_db_conn():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
def get_db_clientconn():
clientconn = sqlite3.connect('database.db')
clientconn.row_factory = sqlite3.Row
return clientconn
def get_post(post_id):
conn = get_db_conn()
post = conn.execute('select * from posts where id =?',(post_id,)).fetchone()
return post
def get_clientpost(clientpost_id):
clientconn = get_db_conn()
clientpost = clientconn.execute('select * from posts where id =?',(clientpost_id,)).fetchone()
return clientpost
#-----------------------------------------------------------------------------------------------------------------------
@app.route('/')
def index():
return render_template('index.html')
@app.route('/posts/page')
def page():
conn = get_db_conn()
posts = conn.execute('select * from posts').fetchall()
return render_template ('page.html',posts=posts)
@app.route('/posts/pageForClient')
def pageForClient():
conn = get_db_conn()
clientposts = conn.execute('select * from posts').fetchall()
return render_template ('pageForClient.html', clientposts=clientposts)
@app.route('/posts/<int:post_id>')
def post(post_id):
post = get_post(post_id)
return render_template('post.html',post=post)
@app.route('/clientposts/<int:clientpost_id>')
def clientpost(clientpost_id):
clientpost = get_clientpost(clientpost_id)
return render_template('clientpost.html',clientpost=clientpost)
@app.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('page'))
return render_template('new.html')
@app.route('/posts/<int:post_id>/edit', methods=('GET','POST'))
def edit(post_id):
post = get_post(post_id)
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
if not title:
flash('Title is required')
else:
conn=get_db_conn()
conn.execute('UPDATE posts SET title = ?, content = ?'
'WHERE id = ?',
(title, content, post_id))
conn.commit()
conn.close()
return redirect(url_for('page'))
return render_template('edit.html', post=post)
@app.route('/posts/<int:post_id>/delete', methods=('POST',))
def delete(post_id):
post = get_post(post_id)
conn = get_db_conn()
conn.execute('DELETE FROM posts WHERE id = ?',(post_id,))
conn.commit()
conn.close()
flash('"{}"删除成功!'.format(post['title']))
return redirect(url_for('page'))