flackとpython構文を使用したhtml


本日のレッスン内容


昨日の授業では、関数的に整理されたhtmlドキュメントに投稿を書く方法と削除する方法について授業が行われました.

難点と解決策


実際、昨日と今日は本人が理解しにくい分野であり、自分がどの程度理解したかを測ることもできません。


昨日と今日書いたコードを見て、私が理解しているのはどこなのか、理解していないのはどこなのかを見たいです.
from flask import Flask, request, redirect /
=>フラスコプログラムを使用し、request、redirect内蔵機能を使用します.
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 ...."}
] 
=>トピックという名前のマルチアイテムリストを作成します.すべてのリンクに重複するhtmlドキュメントの内容にデータ化し、for in文とif文を使用してhtmlドキュメントに挿入します.
nextId = 4
def template(content, id=None):
=>template汎用htmlドキュメントの作成
  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>
        <li>
          <form action="/delete/{id}/" method="POST">
            <input type="submit" value="delete">
          </form>
        </li>
      </ul>
    </body>
  </html>
  '''
** @app.パス(""):(")内のURLに沿って移動できます.
@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}', id)

@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']) 
                              =>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.route('/update/')
# def update():
#   return 'Update'
 

app.run()

感想


実は、昨日と今日私が学んだPython文法がフラスコでこのように使われているのは理解できます.