티스토리 뷰
이걸 골격삼아 사내게시판 등으로 활용할 수 있을 것으로 기대된다.
run.py
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
@app.route("/")
def run():
conn = sqlite3.connect('wanggun.db')
conn.row_factory=sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM general')
rows = c.fetchall();
return render_template("index.html",rows = rows)
@app.route('/modi')
def modi():
id = request.args.get("id")
conn = sqlite3.connect('wanggun.db')
conn.row_factory=sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM general where id='+str(id))
rows = c.fetchall();
return render_template('modi.html',rows = rows)
@app.route('/addrec',methods = ['POST', 'GET'])
def addrec():
if request.method == 'POST':
try:
war = request.form['war']
id = request.form['id']
with sqlite3.connect("wanggun.db") as con:
cur = con.cursor()
cur.execute("update general set war="+str(war)+" where id="+str(id))
con.commit()
msg = "정상적으로 입력되었습니다."
except:
con.rollback()
msg = "입력과정에서 에러가 발생했습니다."
finally:
return render_template("result.html",msg = msg)
con.close()
app.run(host='0.0.0.0', port=8787, debug=True)
\templates\index.html
{% for row in rows %}
<a href='/modi?id={{row[0]}}'>{{row[1]}} </a>
<br>
{% endfor %}
\templates\modi.html
{% for row in rows %}
<form action = "{{ url_for('addrec') }}" method = "POST">
<table>
<tr>
<td>
{{row[1]}}
</td>
<td>
<input type = "text" name = "war" value = {{row[2]}}> <br>
</td>
<br>
<tr>
<td>
<input type=hidden name=id value={{row[0]}}>
<input type = "submit" value = "확인" /><br>
</td>
</form>
{% endfor %}
\templates\result.html
<!doctype html>
<html>
<body>
{{ msg }}
<h2><a href = "\">메뉴로 돌아가기</a></h2>
</body>
</html>