I’ve just started learning Python recently and am using Pyramid as my web framework.
I’m trying to add a static view at localhost/images/misc:
config.add_static_view('images', 'C:/Project/Images/')
config.add_static_view('images/misc', 'C:/Path/To/Other/Images/')
But I get an error: File does not exist: C:/Project/images/misc
So it seems that the second line adding images/misc as a static view doesn’t have any effect.
I’ve been searching for a while for a way to do this, but I haven’t found anything. Is it possible to add a static view where the name contains a subdirectory? If so, how?
Under the hood, pyramid turns the
namepart of theadd_static_view()method into a Pyramid route predicate of the formname/*subpath(wherenamecan contain slashes itself). A dedicated static asset view is attached to that route predicate.In your configuration that means there would be both
images/*subpathandimages/misc/*subpathroute predicates, in that order. When you then request a URL with the pathimages/misc/foo.pngPyramid finds theimages/*subpathpredicate first, and tries to look up themiscpath element in the'C:/Project/Images/'folder, which fails.Lucky for you, you can reverse the order of the registrations, simply register the
images/miscpath first:Now the
images/misc/*subpathpredicate is registered first, and will match anyimages/misc/URLs before the other predicate is tested.