やりたいこと
URL「http://127.0.0.1:5000/detail/1」を開いたときに、トップページの「Detail」をクリックすると、detail.htmlの画面に遷移して、タスクの詳細を表示させたい。
サイトに表示されるエラー
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
**Pythonのターミナルで起こってるエラー** C:\Users\TK\Desktop\TODO-APP\env\lib\site-packages\flask_sqlalchemy\__init__.py:872: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. warnings.warn(FSADeprecationWarning( * Debugger is active! * Debugger PIN: 532-667-604 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [04/Nov/2021 18:06:49] "GET /detail/1 HTTP/1.1" 404 - 127.0.0.1 - - [04/Nov/2021 18:06:53] "GET /detail/1 HTTP/1.1" 404 - 127.0.0.1 - - [04/Nov/2021 18:06:59] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [04/Nov/2021 18:07:01] "GET /detail/2 HTTP/1.1" 404 - 127.0.0.1 - - [04/Nov/2021 18:12:25] "GET /detail/1 HTTP/1.1" 404 -
python
1 2# ファイル名「app.py」 3 4from datetime import datetime 5from flask import Flask, render_template, request, redirect 6from flask_sqlalchemy import SQLAlchemy 7 8app = Flask(__name__) 9app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///todo.db" 10 11db = SQLAlchemy(app) 12 13class Post(db.Model): 14 15 16 id = db.Column(db.Integer, primary_key=True) 17 title = db.Column(db.String(30), nullable=False) 18 detail = db.Column(db.String(100)) 19 due = db.Column(db.DateTime, nullable=False) 20 21@app.route("/", methods=["GET","POST"]) 22 23def index(): 24 25 if request.method == "GET": 26 27 posts = Post.query.all() 28 return render_template("index.html", posts=posts) 29 else: 30 31 title = request.form.get("title") 32 detail = request.form.get("detail") 33 due = request.form.get("due") 34 35 36 due = datetime.strptime(due,"%Y-%m-%d") 37 38 new_post = Post(title=title,detail=detail,due=due) 39 40 41 db.session.add(new_post) 42 db.session.commit() 43 44 return redirect("/") 45 46 47@app.route("/create") 48def create(): 49 50 return render_template("create.html") 51 52if __name__ == "__main__": 53 54 app.run(debug=True) 55 56@app.route("/detail/<int:id>") 57 58 59def read(id): 60 post = Post.query.get(id) 61 return render_template("detail.html", post=post) 62
HTML
1 2<!-- ファイル名「base.html」--> 3 4 5<!DOCTYPE html> 6<html lang="en"> 7<head> 8 <meta charset="UTF-8"> 9 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 10 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 11 <title>Document</title> 12 13 {% block head %}{% endblock %} 14</head> 15<body> 16 {% block body %}{% endblock %} 17</body> 18</html> 19
HTML
1 2<!-- ファイル名「index.html」--> 3 4{% extends "base.html" %} 5 6{% block body %} 7<h1>トップページ</h1> 8{% for post in posts %} 9 10<h2>タイトル : {{ post.title}}</h2> 11<p>期限 : {{ post.due.date()}}</p> 12 13<a href="detail/{{ post.id }}" role="button">Detail</a> 14{% endfor %} 15{% endblock %}
HTML
1 2<!-- ファイル名「detail.html」--> 3 4{% extends "base.html" %} 5 6{% block body %} 7<h2>{{ post.title }}</h2> 8<p>{{ post.detail }}</p> 9<p>{{ post.due.date() }}</p> 10{% endblock %}
HTML
1 2<!-- ファイル名「create.html」--> 3 4{% extends "base.html" %} 5 6{% block body %} 7<form action="/" method="POST"> 8 <label for="title">title</label> 9 <input type="text" name="title"> 10 <label for="detail">Detail</label> 11 <input type="text" name="detail"> 12 <label for="due">Due</label> 13 <input type="date" name="due" required> 14 <input type="submit" value="Create"> 15</form> 16{% endblock %}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/04 13:47