qianfanyimeng 2022-01-11 15:18 采纳率: 100%
浏览 36
已结题

flask博客项目如何实现“查”功能

一个简单的博客项目,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'))

img

img

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 1月19日
      • 已采纳回答 1月11日
      • 创建了问题 1月11日

      悬赏问题

      • ¥15 postman测试正常,在代码运行报错
      • ¥15 关于#C语言#的问题,如何解决?
      • ¥20 Vs2017 Help Viewer2.3 问题
      • ¥35 基于嵌入式linux的日程管理软件
      • ¥50 如何将list字符串添加到CSV文件表头?
      • ¥15 关于#javascript#的问题:通过ajax实现的局部刷新 如何将项目打包
      • ¥15 海思uboot USB3.0无法识别
      • ¥15 无法调用库文件,自己可以找到,但编译时显示没有
      • ¥15 安装PyQt5的时候这里创建虚拟环境是哪里?具体是怎么的?能录个视频吗
      • ¥20 php程序设计题不会!求解答!