index.htmlの編集をクリックすると、どうしても、Not Foundのページになってしまいます。
編集をクリックしたときに、URLには、**http://127.0.0.1:5000/1/update/**と、id/update の形式になっております。
再度、update.html、app.pyを作成しなおしましたが、エラーが解決できない状況です。
どなたか、何処が間違っているのか、確認すべき箇所をご教示いただければと思います。
html
1<!-- index.html --> 2 3{% extends 'base.html' %} 4{% block content %} 5<h1>Top</h1> 6<a href="create" role="buttom">新規作成</a> 7 8{% for post in posts %} 9 <h2>title: {{post.title}}</h2> 10 <p>内容: {{post.body}}</p> 11 <a href="/{{post.id}}/update" role="buttom">編集</a> 12 13{% endfor %} 14{% endblock %}
html
1<!-- update.html --> 2 3{% extends 'base.html' %} 4{% block content %} 5<h1>編集画面</h1> 6<form action="" method="POST"> 7 <label for="title">Title</label> 8 <input type="text" name="title" value={{post.title}}> 9 <label for="body">内容</label> 10 <input type="text" name="body" value={{post.body}}> 11 <input type="submit" value="編集"> 12</form> 13{% endblock %} 14
python
1# app.py 2 3from flask import Flask, render_template, request, redirect 4from flask_sqlalchemy import SQLAlchemy 5from datetime import datetime 6import pytz 7 8 9app = Flask(__name__) 10app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' 11app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 12db = SQLAlchemy(app) 13 14class Post(db.Model): 15 id = db.Column(db.Integer, primary_key = True) 16 title = db.Column(db.String(50), nullable=False) 17 body = db.Column(db.String(300), nullable=False) 18 created_at = db.Column(db.DateTime, nullable=False, default=datetime.now(pytz.timezone('Asia/Tokyo'))) 19 20 21@app.route('/') 22def index(): 23 if request.method == 'GET': 24 posts = Post.query.all() 25 return render_template('index.html', posts=posts) 26 return render_template('index.html') 27 28@app.route('/create', methods=['GET', 'POST']) 29def create(): 30 if request.method == 'POST': 31 title = request.form.get('title') 32 body = request.form.get('body') 33 34 post = Post(title=title, body=body) 35 db.session.add(post) 36 db.session.commit() 37 return redirect('/') 38 return render_template('create.html') 39 40# update(編集)でNot Foundになってしまう。 41@app.route('/<int:id>/update', methods=['GET', 'POST']) 42def update(id): 43 post = Post.query.get(id) 44 45 if request.method == 'GET': 46 return render_template('/update.html', post=post) 47 else: 48 post.title = request.form.get('title') 49 post.body = request.form.get('body') 50 db.session.commit() 51 return redirect('/') 52 53if __name__ == '__main__': 54 app.run(debug=True)


回答1件
あなたの回答
tips
プレビュー