Question

I add the static view like this:

config.add_static_view('CF_static', 'cistromeprimerscanner:templates/static', cache_max_age=604800)

When I want to use a file in static directory in template file, I need to write like this, which is very long :

${request.static_url('cistromeprimerscanner:templates/static/logo.png')}

I was wondering why I shouldn't write it like this:

$(request.static_url("CF_static/logo.png")}

Just as:

${request.route_path("CF_view")}

Does anyone have ideas about this? Thanks!

Was it helpful?

Solution

The doc for static_url specifies that you can use a relative path, but I think that may lead to problems at some point (you don't expect your code to stop working after you move some files).

It's easy to add your own method to request, that would be a shortcut. For example :

def CF_static(request, path):
    return request.static_url('cistromeprimerscanner:templates/static/%s'%pth)
config.add_request_method(CF_static)

Then, you can write :

${request.CF_static('logo.png')}

add_request_method is available in pyramid 1.4. If you don't want to use the alpha, you can use set_request_property instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top