开发一个基于 Flask 的笔记应用,用户可以创建、编辑、删除笔记

这是一个使用Flask框架编写的Python笔记应用示例,用户可以创建、编辑和删除笔记。应用通过不同的路由处理创建、编辑和删除操作,存储笔记数据在一个简单的列表中。
摘要由CSDN通过智能技术生成

以下是一个基于 Flask 的笔记应用的简单实现,可以让用户创建、编辑、删除笔记:

```python
from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

notes = []

@app.route('/')
def index():
    return render_template('index.html', notes=notes)

@app.route('/create', methods=['GET', 'POST'])
def create_note():
    if request.method == 'POST':
        title = request.form['title']
        content = request.form['content']
        notes.append({'title': title, 'content': content})
        return redirect(url_for('index'))
    else:
        return render_template('create.html')

@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit_note(id):
    note = notes[id]
    if request.method == 'POST':
        title = request.form['title']
        content = request.form['content']
        note['title'] = title
        note['content'] = content
        return redirect(url_for('index'))
    else:
        return render_template('edit.html', note=note)

@app.route('/delete/<int:id>')
def delete_note(id):
    del notes[id]
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run(debug=True)
```

在这个应用中,我们定义了四个路由:

- `/`:显示所有笔记的列表,支持创建、编辑和删除笔记。
- `/create`:允许用户创建新的笔记。
- `/edit/<int:id>`:允许用户编辑现有的笔记。
- `/delete/<int:id>`:允许用户删除现有的笔记。

我们使用一个简单的列表来存储笔记,每个笔记包含标题和内容。在 `create_note` 路由中,我们从表单中获取标题和内容,并将新笔记添加到列表中。在 `edit_note` 路由中,我们首先获取指定 ID 的笔记,然后允许用户编辑标题和内容。在 `delete_note` 路由中,我们删除指定 ID 的笔记。

要运行这个应用,需要安装 Flask:

```
pip install flask
```

然后运行以下命令:

```
python app.py
```

在浏览器中访问 `http://localhost:5000/`,就可以看到笔记列表并开始创建、编辑和删除笔记了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A336563

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值