LoginSignup
0
1

Python_Bottleフレームワークで静的ファイルのルーティング設定をする

Posted at

PythonのBottleフレームワークを使用してcssファイルのルーティングを設定したが、
うまくいかなかったので解決策をメモ。

ディレクトリ構成

app
├ bottle.py
├ process.py

├ public
│ └index.html

├ static
│ └style.css

cssが読み込まれなかった時のコード

test.py
# cssファイルを設定する
@route('/static/<filepath:path>')
def server_static(filepath):
	return static_file(filepath, root='./')  # root=どこのパスを設定するのか不明
index.html
<link rel="stylesheet" href="static/style.css">

cssが読み込まれない原因

static_file関数の第二引数であるrootはどこまでのパスを書けば良いかわかっていなかった。

原因の解決方法

rootは'/'の場合、.pyファイルが置かれた場所のことを言う。
今回はstyle.cssの場所を設定しないといけないため、./static/ が正しい。

参考サイト

https://qiita.com/tomotaka_ito/items/377b2287e71ecd8b4f16
http://kitabatech.blogspot.com/2014/07/bottle.html
https://bottlepy.org/docs/dev/tutorial.html#request-routing

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1