glitch에서 웹서버를 열어서 flask를 사용해 입력을 받는 텍스트박스를 만들어 보았습니다. routing과 함수을 정의하여 중복을 막는 코드 사용을 배우고 사용해 보았습니다.
강의 내용은 쉽게 해주시지만 내용 자체가 많아서 받아들이고 익숙해지는데에 시간이 걸릴 것 같습니다!
계속 반복해보기
routing = 길찾기
중복을 제거하는 것이 좋은 코딩!
from flask import Flask
import random
app = Flask(__name__)
topics = [
{"id":1,"title":"html", "body":"html is ..."},
{"id":2,"title":"css", "body":"css is ..."},
{"id":3,"title":"js", "body":"js is ..."}
]
def template(content):
liTags = ''
for topic in topics:
liTags = liTags + f'<li><a href="/read/{topic["id"]}/">{topic["title"]}</a></li>'
return f'''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{liTags}
</ol>
{content}
<ul>
<li><a href="/create/">create</a></li>
</ul>
</body>
</html>
'''
@app.route("/")
def index():
return template('<h2>Welcome</h2>Hello, web')
@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'<h2>{title}</h2> Hello, {body}')
@app.route('/create/')
def create():
content='''
<form action="/create/">
<p><input text="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('/update/')
def update():
return 'Update'
app.run()