[DAY8]Python_Flask
25352 ワード
Flashはウェブページを印刷する工場です。
-関連サイト:Glitch.com
+会員加入後コード作成開始
+コード変更をリアルタイムで表示
今日はサーバーpyにコードを書きました.
<今日コード>from flask import Flask
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>{body}')
@app.route('/create/')
def create():
content = '''
<form action="/create/">
<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('/update/')
def update():
return 'Update'
app.run()
-ルーターは?
<難点>
コード内のエラーにすぐに対応するのは難しい.
ex)-正しいコード-from flask import Flask
app = Flask(__name__)
topics = [
{"id":1, "title":"html", "body":"html is ...."},
{"id":2, "title":"css", "body":"css is ...."}
]
@app.route("/")
def index():
liTags = ''
for topic in topics:
liTags = liTags + f'<li>{topic["title"]}</li>'
return f'''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{liTags}
</ol>
<h2>Welcome</h2>
Hello, WEB!
</body>
</html>
'''
@app.route("/read/1/")
def read():
return '''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
<li><a href="/read/1/">html</a></li>
<li><a href="/read/2/">css</a></li>
<li><a href="/read/3/">js</a></li>
</ol>
<h2>Read</h2>
Hello, Read!
</body>
</html>
'''
@app.route('/create/')
def create():
return 'Create'
@app.route('/update/')
def update():
return 'Update'
app.run()
![](https://media.vlpt.us/images/monica8842/post/dccdfc11-9bbe-4d18-9072-b0ad6e901670/image.png)
-エラーコード-from Flask import Flask
app = Flask(__name__)
topics = [
{"id":1, "title":"html", "body":"html is ...."},
{"id":2, "title":"css", "body":"css is ...."}
]
@app.route("/")
def index():
liTags = ''
for topic in topics:
liTags = liTags + f'<li>{topic["title"]}</li>'
return f'''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{liTags}
</ol>
<h2>Welcome</h2>
Hello, WEB!
</body>
</html>
'''
@app.route("/read/1/")
def read():
return '''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
<li><a href="/read/1/">html</a></li>
<li><a href="/read/2/">css</a></li>
<li><a href="/read/3/">js</a></li>
</ol>
<h2>Read</h2>
Hello, Read!
</body>
</html>
'''
@app.route('/create/')
def create():
return 'Create'
@app.route('/update/')
def update():
return 'Update'
app.run()
![](https://media.vlpt.us/images/monica8842/post/70bb5ecf-44cc-47f2-9100-75e2b907ba48/image.png)```
코드를 입력하세요
from flask import Flask
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>{body}')
@app.route('/create/')
def create():
content = '''
<form action="/create/">
<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('/update/')
def update():
return 'Update'
app.run()
from flask import Flask
app = Flask(__name__)
topics = [
{"id":1, "title":"html", "body":"html is ...."},
{"id":2, "title":"css", "body":"css is ...."}
]
@app.route("/")
def index():
liTags = ''
for topic in topics:
liTags = liTags + f'<li>{topic["title"]}</li>'
return f'''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{liTags}
</ol>
<h2>Welcome</h2>
Hello, WEB!
</body>
</html>
'''
@app.route("/read/1/")
def read():
return '''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
<li><a href="/read/1/">html</a></li>
<li><a href="/read/2/">css</a></li>
<li><a href="/read/3/">js</a></li>
</ol>
<h2>Read</h2>
Hello, Read!
</body>
</html>
'''
@app.route('/create/')
def create():
return 'Create'
@app.route('/update/')
def update():
return 'Update'
app.run()
![](https://media.vlpt.us/images/monica8842/post/dccdfc11-9bbe-4d18-9072-b0ad6e901670/image.png)
from Flask import Flask
app = Flask(__name__)
topics = [
{"id":1, "title":"html", "body":"html is ...."},
{"id":2, "title":"css", "body":"css is ...."}
]
@app.route("/")
def index():
liTags = ''
for topic in topics:
liTags = liTags + f'<li>{topic["title"]}</li>'
return f'''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{liTags}
</ol>
<h2>Welcome</h2>
Hello, WEB!
</body>
</html>
'''
@app.route("/read/1/")
def read():
return '''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
<li><a href="/read/1/">html</a></li>
<li><a href="/read/2/">css</a></li>
<li><a href="/read/3/">js</a></li>
</ol>
<h2>Read</h2>
Hello, Read!
</body>
</html>
'''
@app.route('/create/')
def create():
return 'Create'
@app.route('/update/')
def update():
return 'Update'
app.run()
![](https://media.vlpt.us/images/monica8842/post/70bb5ecf-44cc-47f2-9100-75e2b907ba48/image.png)```
코드를 입력하세요
『学習心得』
htmlの復習を続けていると、慣れてきたような気がします.
Pythonまで身につけるよりも、こんなに多様なコードが勉強されていると思っています.
Reference
この問題について([DAY8]Python_Flask), 我々は、より多くの情報をここで見つけました https://velog.io/@monica8842/DAY-8PythonFlaskテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol