Quantcast
Channel: 一言多いプログラマーの独り言
Viewing all articles
Browse latest Browse all 846

Python+Flaskに、ORMのSQLAlchemyを設定、プライマリーキーでデータを取得

$
0
0
Python+Flaskに、ORMのSQLAlchemyを設定、プライマリーキーでデータを取得です。get(id)が基本で、get_or_404(id)だと、データが見つからない場合、404を返してくれるらしい。


@app.route('/blog/<int:blog_id>')
def show_blog(blog_id):
entry = Entry.query.get_or_404(blog_id)
return render_template('show_blog.html', entry=entry)

実際に設定してみたのがこちら

参考サイト
Select, Insert, Delete(Flask-SQLAlchemy Documentation)
Python+Flaskに、ORMのSQLAlchemyを設定

Viewing all articles
Browse latest Browse all 846

Trending Articles