Auto commit

上级 df352508
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"
......
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/<int:id>', 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/<int:id>')
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
文件已添加
print('欢迎来到 InsCode')
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>增加笔记</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>增加笔记</h1>
<form method="POST">
<div class="form-group">
<label>标题</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label>内容</label>
<textarea class="form-control" rows="10" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary">提交</button>
</form>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>编辑笔记</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>编辑笔记</h1>
<form method="POST">
<div class="form-group">
<label>标题</label>
<input type="text" class="form-control" name="title" value="{{ note.title }}">
</div>
<div class="form-group">
<label>内容</label>
<textarea class="form-control" rows="10" name="content">{{ note.content }}</textarea>
</div>
<button type="submit" class="btn btn-primary">提交</button>
</form>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>笔记 {% if q %} - 搜索结果:{{ q }}{% endif %}</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>笔记</h1>
<form class="form-inline justify-content-center" method="GET">
<input type="text" class="form-control form-control-success" name="q" {% if q %} value="{{ q }}" {% endif %} placeholder="输入标题或内容搜索">
<button type="submit" class="btn btn-success">
<span class="oi oi-magnifying-glass"></span>搜索
</button>
</form>
{% if q %}
<a href="/" class="btn btn-light">清除搜索</a>
{% endif %}
<a href="/add" class="btn btn-primary">增加笔记</a>
<hr>
{% if q %}
<h3>搜索结果:{{ q }}</h3>
{% endif %}
{% for note in notes %}
<div class="card">
<div class="card-header">
<h3>{{ note.title }}</h3>
<a href="/edit/{{ note.id }}" class="btn btn-secondary">编辑</a>
<a href="/delete/{{ note.id }}" class="btn btn-danger" onclick="return confirm('你确定删除吗?')">删除</a>
</div>
<div class="card-body">
<p>{{ note.content }}</p>
</div>
</div>
<hr>
{% endfor %}
</div>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册