How do I call a cache busting function inside request.static_url() in my layout.pt chameleon template

145 views Asked by At

I'm using a Cookiecutter Pyramid web development framework. This has three linked files:

utils.py - defines the random cache string to add to an url

views.py - passes utils functions to chameleon template

layout.pt - chameleon template calling static css urls etc.

In layout.pt pre-cache-busting there is a link that says:

<link href="${request.static_url('pycharm_app:static/theme.css')}" rel="stylesheet">

I'm trying to edit this link to call the build_cache_id function as so:

 <link href="/static/theme.css?cacheId=${build_cache_id('/static/theme.css')}" rel="stylesheet">

but get error:

NameError: build_cache_id

 - Expression: "build_cache_id('/static/theme.css')"
 - Filename:   ... s/first_business_website/pycharm_app/templates/layout.pt
 - Location:   (line 17: col 43)
 - Source:     ... ss?cacheId=${build_cache_id('/static/theme.css')}" rel="styl ...
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 - Expression: "load: layout.pt"
 - Filename:   ... rst_business_website/pycharm_app/templates/mytemplate.pt
 - Location:   (line 1: col 22)
 - Source:     <div metal:use-macro="load: layout.pt">
                                     ^^^^^^^^^^^^^^^
 - Arguments:  view: <function my_view at 0x109eff8b0>
               renderer_name: ../templates/mytemplate.pt
               renderer_info: <RendererHelper - at 0x109f3f040>
               context: <DefaultRootFactory None at 0x109d3faf0>
               request: <Request - at 0x109d3fa90>
               req: <Request - at 0x109d3fa90>
               get_csrf_token: <partial - at 0x109f32db0>
               project: pycharm_app
               target_language: <NoneType - at 0x10817de60>
               repeat: {...} (0)
               macroname: load: layout.pt
2

There are 2 answers

8
sinoroc On

The Arguments part of the error output clearly shows that build_cache_id is not passed to the template engine. On the other hand, the project variable is.

 - Arguments:  view: <function my_view at 0x109eff8b0>
               renderer_name: ../templates/mytemplate.pt
               renderer_info: <RendererHelper - at 0x109f3f040>
               context: <DefaultRootFactory None at 0x109d3faf0>
               request: <Request - at 0x109d3fa90>
               req: <Request - at 0x109d3fa90>
               get_csrf_token: <partial - at 0x109f32db0>
               project: pycharm_app
               target_language: <NoneType - at 0x10817de60>
               repeat: {...} (0)
               macroname: load: layout.pt```

Looks like the extend_model function is not correctly called, even though the code looks correct to me. Was the application correctly reloaded? Maybe add simple print calls in these functions as a quick and dirty debug and make sure that the correct code is running.

0
thunt On

Fixed this! with a lot of help from @sinoroc.

The problem is that using PyCharm to initiate a pyramid web project sets up files and folder structure in a different way to cookiecutter via terminal. It is not immediately possible to copy across "missing" files from the cookiecutter framework to the PyCharm framework without ensuring you fix the plumbing. - typing proutes development.ini in terminal will list routes. In my instance there were routes for pyramid_debugtoolbar with no url. Deleting pyramid_debugtoolbar from development.ini fixed this issue. - All routes need to be specified in __init__.py file. Copy pasting new template files in to the folder structure does not mean they will automatically appear here. - All routes need to be passed to @view_config in views.py. E.g.

@view_config(route_name='index',renderer='templates/index.pt')
def index(_):
return extend_model({'project':'designable_web'})

Again, these routes will not automatically be set up when pasting in new file names from another folder structure.

  • Check which template language is installed in setup.py (e.g. chameleon, jinja2). At present PyCharm has a bug that defaults to jinja2 EVEN IF YOU SELECTED CHAMELEON ON SET UP. Template files will not work unless the correct corresponding language is specified here.

  • Check folder depth. The cookiecutter framework had a separate views folder with views file inside, whereas PyCharm had the views.py file sitting underneath the top "app level" folder. Again proutes development.ini will specify which is correct.