학습한 코드
from flask import Flask, request, redirect
flask 실행 코드
**from flask import flask
app = flask(name)
**
route 태그페이지 담당자 지정 태그
@app.route("/")
def index():
return template('
topics = [
{"id":1, "title":"html", "body":"html is ...."},
{"id":2, "title":"css", "body":"css is ...."},
{"id":3, "title":"js", "body":"js is ...."}
]
nextId = 4
def template(content, id=None):
liTags = ''
for topic in topics:
liTags = liTags + f'
WEB
{liTags}
{content} '''
@app.route("/")
def index():
return template('
@app.route("/read/<int:id>/")
def read(id):
title = ''
body = ''
for topic in topics :
if topic['id'] == id:
title = topic['title']
body = topic['body']
break;
return template(f'
@app.route('/create/')
def create():
content = '''
<form action="/create_process/" method="POST">
<p><input type="text" name="title" placeholder="title"></p>
<p><textarea name="body" placeholder="body"></textarea></p>
<p><input type="submit" value="create"></p>
</form>
'''
return template(content)
@app.route('/create_process/', methods=['POST'])
def create_process():
global nextId
title = request.form['title']
body = request.form['body']
newTopic = {"id":nextId, "title": title, "body": body}
topics.append(newTopic)
nextId = nextId + 1
return redirect(f'/read/{nextId-1}/')
@app.route('/delete/<int:id>/', methods=['POST'])
def delete(id):
for topic in topics:
if topic['id'] == id:
topics.remove(topic)
break;
return redirect('/')
app.run()
SQLite3가 손에 익지않아 어렵다.. 호출하고 생성하는것이 무척 힘이든다.
영상복습하는 것밖에 답이없다.. 생활코딩 홈페이지도 참고많이 하고 구글링..
SQL을 이용해 표를 만들고 데이터를 입력하는 게 어려웠고 갈수록 더 헤메는 중이다..