diff --git a/.inscode b/.inscode index c9c57cedade1a0283ef70fba9bf9b5c80fc198a2..f6aad02e560e878f5a42e71d825eb4cec86e7141 100644 --- a/.inscode +++ b/.inscode @@ -1,4 +1,4 @@ -run = "pip install -r requirements.txt;python3 main.py" +run = "pip install -r requirements.txt;python3 app.py" [env] VIRTUAL_ENV = "/root/${PROJECT_DIR}/venv" diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..45ebb18c783f5bbec43342e066329cd576b11556 --- /dev/null +++ b/app.py @@ -0,0 +1,53 @@ +from flask import Flask, render_template, request, redirect +from flask_sqlalchemy import SQLAlchemy + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///notes.db' +db = SQLAlchemy(app) + +class Note(db.Model): + id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String(100)) + content = db.Column(db.Text) + +@app.route('/') +def index(): + q = request.args.get('q') # 获取搜索条件 + if q: + notes = Note.query.filter(Note.title.contains(q) | Note.content.contains(q)) + else: + notes = Note.query.all() + return render_template('index.html',q=q, notes=notes) + +@app.route('/add', methods=['GET', 'POST']) +def add(): + if request.method == 'POST': + title = request.form['title'] + content = request.form['content'] + note = Note(title=title, content=content) + db.session.add(note) + db.session.commit() + return redirect('/') + return render_template('add.html') + +@app.route('/edit/', methods=['GET', 'POST']) +def edit(id): + note = Note.query.get(id) + if request.method == 'POST': + note.title = request.form['title'] + note.content = request.form['content'] + db.session.commit() + return redirect('/') + return render_template('edit.html', note=note) + +@app.route('/delete/') +def delete(id): + note = Note.query.get(id) + db.session.delete(note) + db.session.commit() + return redirect('/') + +if __name__ == '__main__': + with app.app_context(): + db.create_all() + app.run(port=5001,debug=True) \ No newline at end of file diff --git a/instance/notes.db b/instance/notes.db new file mode 100644 index 0000000000000000000000000000000000000000..dd7768cf39dfdb05f74b8399559023e12f5e70b7 Binary files /dev/null and b/instance/notes.db differ diff --git a/main.py b/main.py deleted file mode 100644 index 4c0c135f61696bcf42c375ca5ab62aa5b105afc8..0000000000000000000000000000000000000000 --- a/main.py +++ /dev/null @@ -1 +0,0 @@ -print('欢迎来到 InsCode') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c55744dc3459fb6e066c3f4f7dbc59967bdc5f75 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +flask_sqlalchemy \ No newline at end of file diff --git a/templates/add.html b/templates/add.html new file mode 100644 index 0000000000000000000000000000000000000000..9938a9ec1c668bf1b79a32eee5ad708d097a6ae4 --- /dev/null +++ b/templates/add.html @@ -0,0 +1,26 @@ + + + + + 增加笔记 + + + + +
+

增加笔记

+
+
+ + +
+
+ + +
+ +
+
+ + + \ No newline at end of file diff --git a/templates/edit.html b/templates/edit.html new file mode 100644 index 0000000000000000000000000000000000000000..108d23b0c920bc5e00c483b452cd9ae99895db8f --- /dev/null +++ b/templates/edit.html @@ -0,0 +1,26 @@ + + + + + 编辑笔记 + + + + +
+

编辑笔记

+
+
+ + +
+
+ + +
+ +
+
+ + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000000000000000000000000000000000000..4d21b5fc7615a54118b93092b68ff429bfb70679 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,42 @@ + + + + + 笔记 {% if q %} - 搜索结果:{{ q }}{% endif %} + + + + +
+

笔记

+
+ + +
+ {% if q %} + 清除搜索 + {% endif %} + 增加笔记 +
+ {% if q %} +

搜索结果:{{ q }}

+ {% endif %} + {% for note in notes %} +
+
+

{{ note.title }}

+ 编辑 + 删除 +
+
+

{{ note.content }}

+
+
+
+ {% endfor %} +
+ + + \ No newline at end of file