Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

about: add_static_view #3297

Closed
jgmtzmd opened this issue May 21, 2018 · 9 comments
Closed

about: add_static_view #3297

jgmtzmd opened this issue May 21, 2018 · 9 comments

Comments

@jgmtzmd
Copy link

jgmtzmd commented May 21, 2018

hi.
building a site i could not make use of the static views.
long story short: i started from zero using the starter cookiecutter and realized that the add_static_view does not work for subfolders of "../templates". for any other location works fine.
is this a bug?
is it supposed to work this way? if so, i suggest to include this advice in the docs.
regards

@mmerickel
Copy link
Member

Could you paste an actual example? There's no reason you can't serve subfolders of templates as static assets... whether that's a good idea would be a separate discussion.

@jgmtzmd
Copy link
Author

jgmtzmd commented May 21, 2018

of course.
as i said this is the pyramid-starter cookiecutter plus some silly lines for this test.

this is the tree..

project
├── __init__.py
├── otra
│   └── otra
│       └── logo.bmp
├── __pycache__
│   ├── __init__.cpython-36.pyc
│   ├── tests.cpython-36.pyc
│   └── views.cpython-36.pyc
├── static
│   ├── pyramid-16x16.png
│   ├── pyramid.png
│   └── theme.css
├── templates
│   ├── layout.pt
│   ├── mytemplate.pt
│   └── other
│       └── logo.bmp
├── tests.py
└── views.py

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.add_static_view('static', 'static',cache_max_age=3600)
    """ start of my static views"""
    config.add_static_view('templates', 'in_templates')
    config.add_static_view('templates/other', 'in_templates2')
    config.add_static_view('otra', 'in_otra')
    config.add_static_view('otra/otra', 'in_otra2')
    """ end of my static views"""
    config.add_route('home', '/')
    config.add_route('other', '/other')
    config.scan()
    return config.make_wsgi_app()

views.py is untouched, just as it came in the cookiecutter..

from pyramid.view import view_config
@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    return {'project': 'project'}

and in layout.pt (that comes in cookiecutter i just added 4 images trying to use the static_views.
for brevity i just write those lines here..

..
<body>
    <img src="${request.static_url('project:in_templates/other/logo.bmp')}" alt="00"></img>
    <img src="${request.static_url('project:in_templates2/logo.bmp')}" alt="01"></img>
    <img src="${request.static_url('project:in_otra/otra/logo.bmp')}" alt="02"></img>
    <img src="${request.static_url('project:in_otra2/logo.bmp')}" alt="03"></img>
..

and the result is that i can see only the 3rd and 4th images, only those in templates directory cant be displayed.
the same happens if i use "request.static_path" instead of "request.static.url". i dont know if this is somehow equivalent.

@stevepiercy
Copy link
Member

First, please review community guidelines for how to get support. You've posted to the smallest audience out of all others.
https://pylonsproject.org/community-support.html

Second, I've never tried putting template files and static assets in the same directory, but I assume that doing so would allow the templates to be served as a static asset. I don't do it because it's more configuration than necessary.

Finally, you've got your pyramid.config.Configurator.add_static_view configuration arguments swapped. The name goes first, then the path, and finally any **kw.

    config.add_static_view('in_templates2', 'templates/other')

Read more in the narrative documentation about Static Assets.

@jgmtzmd
Copy link
Author

jgmtzmd commented May 21, 2018 via email

@stevepiercy
Copy link
Member

do you want me to close this ticket and open it in stackoverflow?

You would get a larger audience on SO. I can't follow your issue or reproduce it with the information provided. If you go that route, include a MCVE, such as a link to a GitHub repo, and what steps you take to cause a crash.

@mmerickel
Copy link
Member

mmerickel commented May 22, 2018

being this way, the name is not used.

I think you should see that the generated URL is to /in_templates2/...

The naming in add_static_view is very confusing and requires me to look at the docs almost every time I use it. The "name" is actually the url and the "path" is actually the asset spec pointing to your code. You use the "path" when invoking request.static_url and it will output a url using the name. I know - it's rough.

@mmerickel
Copy link
Member

Closing this as it seems you have something working... we can re-open if there's anything further but I would also suggest posting to SO first as @stevepiercy suggested so you can get a wider audience.

@piotr-dobrogost
Copy link

The naming in add_static_view is very confusing (…)

How about fixing this by adding it to the list in issue #2362 ?

@mmerickel
Copy link
Member

You're welcome to add it but I'll warn you that Pyramid 2.0 is not going to break bw-compat lightly - something like naming of a var is unlikely to get fixed by just swapping the names. The best path forward here is to propose new names and migrate toward them with a deprecation strategy - or just live with it like people have for years :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants