必要なもの一覧
イベント登録画面
event.html
<!DOCTYPE html> <html lang="jp"> <head> <meta charset="UTF-8"> <title>メインページ</title> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta name="description" content="pythonとpythonanywhereのサンプルページです" > <meta name="keywords" content="プログラム,教育,python,pythonanywhere,bootstrap,flask" > <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css')}}"> </head> <body> <div class="container"> <nav class="navbar w-100 navbar-expand-sm navbar-dark bg-dark" aria-label="Third navbar example"> <div class="container-fluid"> <a class="navbar-brand" href="#">Event Photos</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarsExample03" aria-controls="navbarsExample03" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarsExample03"> <ul class="navbar-nav me-auto mb-2 mb-sm-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="/">Home</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="dropdown03" data-bs-toggle="dropdown" aria-expanded="false">操作</a> <ul class="dropdown-menu" aria-labelledby="dropdown03"> <li><a class="dropdown-item" href="/event">イベント追加</a></li> <li><a class="dropdown-item" href="/logout">ログアウト</a></li> </ul> </li> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="/">Help</a> </li> </ul> </div> </div> </nav> <main> <div id="wrapper"> <header class="d-flex justify-content-end"> <p>ID: {{id}} </p> </header> <div class="py-5 text-center"> {% if edata is defined %} <h2>イベント編集</h2> <p class="lead">イベントを編集をします。</p> {% set ekey = edata[0].ekey %} {% set eventname = edata[0].eventname %} {% set email = edata[0].email %} {% set limitdate = edata[0].limitdate %} {% set comment = edata[0].comment %} {% set extendLimit = extendLimit %} {% set editflag = "disable" %} {% else %} <h2>新規イベント登録</h2> <p class="lead">あなたがマスターとなって、新規のイベントを作成します。参加希望が来たら許可、または拒否の選択をしてください</p> {% set eventname = "" %} {% set email = "" %} {% set firstDay = firstDay %} {% set limitdate = limit %} {% set comment = "" %} {% set editflag = "required" %} {% endif %} </div> <div class="row"> <div class="col-md-12 col-lg-12"> <h4 class="mb-3">イベント情報</h4> <form action="/newevent" method="POST" > <div class="row g-3"> <div class="col-12"> <label for="eventname" class="form-label">イベント名</label> <input type="text" value="{{eventname}}" class="form-control" name="eventname" placeholder="Eventname" required> </div> <div class="col-12"> <label for="email" class="form-label">マスターEmail <span class="text-muted">(Optional)</span></label> <input type="email" value="{{email}}" class="form-control" name="email" placeholder="you@example.com"> </div> <div class="col-12"> {% if edata is defined %} <label for="limitdate" class="form-label">イベント有効期限(延長可能)</label> <input type="date" value="{{limitdate}}" class="form-control" name="limitdate" placeholder="2022-11-1" min="{{firstDay}}" max="{{extendLimit}}" {{editflag}}> {% else %} <label for="limitdate" class="form-label">イベント有効期限</label> <input type="date" value="{{limitdate}}" class="form-control" name="limitdate" placeholder="2022-11-1" min="{{firstDay}}" max="{{limitdate}}" {{editflag}}> {% endif %} </div> <div class="col-12"> <label for="comment" class="form-label">コメント</label> <input type="text" value="{{comment}}" class="form-control" name="comment" placeholder="イベントの内容" required> </div> {% if edata is defined %} <input type="hidden" value="{{ekey}}" name="ekey" > <button class="w-100 btn btn-primary btn-lg" type="submit" name="edit" value="edit">編集</button> <button class="w-100 btn btn-danger btn-lg" type="submit" name="delete" value="delete" onclick="return confirm('Delete Events & events photo files?');">イベント削除</button> {% else %} <button class="w-100 btn btn-primary btn-lg" type="submit" name="make" value="make">作成</button> {% endif %} <button class="w-100 btn btn-secondary btn-lg" type="submit" name="cancel" value="cancel" formnovalidate>キャンセル</button> </div> </form> </div> </div> </div> </main> </div> <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script> </body> </html>
event.htmlは新規と編集を同じ画面で切り分けておこなっています。edataつまりエベント情報が渡ってきていれば編集、なければ新規として行っています。
app イベント登録部分
app.py(抜粋)
@app.route("/newevent", methods = ["POST"]) def saveEvent(): # 新規または編集の保存 eventname = request.form.get("eventname") email = request.form.get("email") limitdate = request.form.get("limitdate") comment = request.form.get("comment") events = Event(con,cur) limitStr = events.fmtDate(limitdate,"-") if request.form.get("make"): # 新規登録 t = datetime.date.today() patic = [session["id"]] events.newEvent(session["id"], eventname, t.strftime("%Y-%m-%d"), limitStr, comment, email, patic) return redirect(url_for("top")) elif request.form.get("edit"): # 編集 key = request.form.get("ekey") edataorg = events.getEvent(key) patic = edataorg[0]["paticip"] events.updateEvent(key, session["id"], eventname, limitStr, comment, email, patic) return redirect(url_for("top")) elif request.form.get("delete"): # 削除 key = request.form.get("ekey") events.deleteEvent(key) return redirect(url_for("top")) else: return redirect(url_for("top")) @app.route("/event/", methods = ["GET"]) def editEvent(ekey): # 編集の入り if "id" in session: # 編集の読み込み if ekey: events = Event(con,cur) edata = events.getEvent(ekey) t = datetime.date.today() extendLimit = t + datetime.timedelta(days=7) return render_template("event.html", id=session["id"],edata=edata,extendLimit=extendLimit) else: return render_template("login.html") @app.route("/event", methods = ["GET"]) def newEvent(): # 新規の入り if "id" in session: t = datetime.date.today() firstDay = t.strftime("%Y-%m-%d") defaultLimit = t + datetime.timedelta(days=10) return render_template("event.html", id=session["id"],firstDay=firstDay,limit=defaultLimit) else: return render_template("login.html")
編集の入り口はeditEvent、新規の入り口はnewEventでそれぞれ必要データを読み込んでからevent.htmlに飛びます。そこあとの登録がsaveEventで行われ、内部で新規と編集で分岐しています。