金字塔静态视图(js、img、css):AttributeError
Pyramid static views (js, img, css): AttributeError
为了为我的网络项目提供样式表、图像和 JavaScript 文件,我在 Pyramid 的 static
文件夹中创建了一些子目录,如下所示:
myproject/static/
├── css
│ └── overwrite.css
├── img
├── js
├── pyramid-16x16.png
├── pyramid.png
├── theme.css
└── theme.min.css
但是,使用 Pyramid Cookbook 中所述的 Pyramid 特定 config.add_xyz_view
方法会为所有这三个命令引发 AttributeErrors
,如下所示:
python3.4/site-packages/pyramid/config/init.py", line 793, in
getattr
raise AttributeError(name)
AttributeError: add_images_view
或 css
的等价物
AttributeError: add_stylesheets_view
对于 js
AttributeError: add_javascript_view
目前我正在使用一种解决方法,它似乎很有用(请参阅下面代码中的注释)。
由于我是使用 Pyramid 的初学者,因此使用此解决方法似乎是可以接受的。但是,我想了解这些错误的原因是什么。
项目的 __init__.py
如下所示:
from pyramid.config import Configurator
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.include('pyramid_jinja2')
config.add_static_view('static', 'static', cache_max_age=3600)
# raises AttributeError
# config.add_images_view('img', 'static/img')
# config.add_stylesheets_view('css', 'static/css')
# config.add_javascript_view('js', 'static/js')
# current workaround, works like a charm
config.add_static_view('img', 'static/img', cache_max_age=3600)
config.add_static_view('css', 'static/css', cache_max_age=3600)
config.add_static_view('js', 'static/js', cache_max_age=3600)
config.add_route('home', '/')
config.add_route('foo', '/greet')
config.add_route('bs', '/bs')
config.scan()
return config.make_wsgi_app()
打开 new issue on Pyramid's GitHub repo and showed up, that there seemed to be a mistake in the Pyramid Cookbook,我在 SO 上提出问题后立即修复。我在 repo 上的问题。
所以正确的方法就像我的问题中给出的'workaround':
config.add_static_view('img', 'static/img')
config.add_static_view('css', 'static/css')
config.add_static_view('js', 'static/js')
为了为我的网络项目提供样式表、图像和 JavaScript 文件,我在 Pyramid 的 static
文件夹中创建了一些子目录,如下所示:
myproject/static/
├── css
│ └── overwrite.css
├── img
├── js
├── pyramid-16x16.png
├── pyramid.png
├── theme.css
└── theme.min.css
但是,使用 Pyramid Cookbook 中所述的 Pyramid 特定 config.add_xyz_view
方法会为所有这三个命令引发 AttributeErrors
,如下所示:
python3.4/site-packages/pyramid/config/init.py", line 793, in getattr raise AttributeError(name)
AttributeError: add_images_view
或 css
的等价物AttributeError: add_stylesheets_view
对于 js
AttributeError: add_javascript_view
目前我正在使用一种解决方法,它似乎很有用(请参阅下面代码中的注释)。
由于我是使用 Pyramid 的初学者,因此使用此解决方法似乎是可以接受的。但是,我想了解这些错误的原因是什么。
项目的 __init__.py
如下所示:
from pyramid.config import Configurator
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.include('pyramid_jinja2')
config.add_static_view('static', 'static', cache_max_age=3600)
# raises AttributeError
# config.add_images_view('img', 'static/img')
# config.add_stylesheets_view('css', 'static/css')
# config.add_javascript_view('js', 'static/js')
# current workaround, works like a charm
config.add_static_view('img', 'static/img', cache_max_age=3600)
config.add_static_view('css', 'static/css', cache_max_age=3600)
config.add_static_view('js', 'static/js', cache_max_age=3600)
config.add_route('home', '/')
config.add_route('foo', '/greet')
config.add_route('bs', '/bs')
config.scan()
return config.make_wsgi_app()
打开 new issue on Pyramid's GitHub repo and
所以正确的方法就像我的问题中给出的'workaround':
config.add_static_view('img', 'static/img')
config.add_static_view('css', 'static/css')
config.add_static_view('js', 'static/js')