albert
albert

Reputation: 8593

Pyramid static views (js, img, css): AttributeError

To serve stylesheets, images and JavaScript files for my webproject I created some subdirectories to Pyramid's static folder like shown below:

myproject/static/
├── css
│   └── overwrite.css
├── img
├── js
├── pyramid-16x16.png
├── pyramid.png
├── theme.css
└── theme.min.css

However, using Pyramid's specific config.add_xyz_view methods as stated in the Pyramid Cookbook raises AttributeErrors for all of those three commands like this:

python3.4/site-packages/pyramid/config/init.py", line 793, in getattr raise AttributeError(name)

AttributeError: add_images_view

or equivalent for css

AttributeError: add_stylesheets_view

and for js

AttributeError: add_javascript_view

Currently I am using a workaround, which seems to work like a charm (see comments in code below).

Since I am a beginner using Pyramid working with this workaround seems to be acceptable. However, I would like to understand what's the reason for those errors.

The project's __init__.py looks like the following:

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()

Upvotes: 2

Views: 262

Answers (1)

albert
albert

Reputation: 8593

Opening a new issue on Pyramid's GitHub repo and Steve Piercy's comment showed up, that there seemed to be a mistake in the Pyramid Cookbook, which was immediately fixed after my question here on SO resp. my issue on the repo.

So the correct approach is like the 'workaround' given in my question:

config.add_static_view('img', 'static/img')
config.add_static_view('css', 'static/css')
config.add_static_view('js', 'static/js')

Upvotes: 1

Related Questions