I am trying to run CKAN Extension in CKAN 2.9, but I am receiving an error.
Seems that the problem is wiht jinja2 and with c.linkResource class.
File "/usr/lib/ckan/venv/src/ckanext-relation-sddi/ckanext/relation/templates/package/snippets/resource_form.html", line 5, in top-level template code
{% set action = form_action or h.url_for(c.linkResource) %}
File "/usr/lib/ckan/venv/src/ckan/ckan/lib/helpers.py", line 371, in url_for
if kw.get('controller') == 'api' or args and args[0].startswith('api.'):
jinja2.exceptions.UndefinedError: 'werkzeug.local.LocalProxy object' has no attribute 'linkResource'
Can someone help or give a hint where I can check?
It is important to say that the extension is running on ckan 2.7.
In requirements the extension used Jinja2==2.11.3 and I changed it to Jinja2==3.1.2.
Related
I am new to Django and hence not having thorough knowledge about it. So I am facing a few errors in Django.
Currently I am trying to print the type of a variable from the Django template html file as follows:
<center><h2>The type of feature list report for version {%type(version)%} is<h2></center>
For the above, I am getting the following error:
Invalid block tag on line 9: 'type(version)'. Did you forget to register or load this tag?
So what's going wrong here? How can we use the python related api's (like type(), strip(), get(), etc) from the html template files? I think inside the {% .... %} we can use python related evaluations. Am I right?
Please throw some lights on this .
As said, this is not the philosophy of DTL, but some functions that transform input are implemented as filters.
In addition, you can write your own filters and supporting a "type" filter, would be very simple:
from django import template
from typing import Any
register = template.Library()
def filter_type(value: Any) -> str:
return str(type(value))
register.filter('type', filter_type)
See the documentation for details.
Both Jinja's and DTL's approach are explicit over implicit: instead of blindly supporting any python function with all it's dangers, you have to explicitly allow it or implement it.
Running arbitrary Python code in a Django template is intentionally disabled. Aside from security concerns, the reason is your project's business logic should be separate from your presentation layer. This is part of good application design.
There are three primary ways you can call an operation from a Django template.
Pass in a function and call it.
Use a template filter, either custom or built in.
Use a template tag, either custom or built in.
Pass in a function and call it.
Calling a passed in function from a Django template is standard. However, it has two caveats.
The function must return a value that can is a string or can be coerced to a string. (Otherwise nothing will be printed in the template.)
The function must not have any required arguments.
The most common use case is either a computed value or a getter e.g.
class Page(models.Model):
title = models.CharField()
def get_title(self):
return self.title
<h1>{{ page.get_title }}</h1>
Template filters
See Melvyn's answer for an example of template filters.
Template filters operate on a value. So this is perfect for a Python function like type().
Template Tags
Edited: see Melvyn's comment.
Simple Template tags on the other hand work more like a function. They accept positional and keyword arguments and should again return a value. I won't go into inclusion tags or advanced tag compilation and rendering here, but you can read about it in the Django custom template tag docs.
Here is an example of two template tags I often include in a project in debug.py.
import pprint
from django import template
register = template.Library()
pp = pprint.PrettyPrinter(indent=4, width=120)
#register.simple_tag(takes_context=True)
def print_context(context):
pp.pprint(context)
return ""
#register.simple_tag()
def print_thing(thing):
pp.pprint(thing)
return ""
I can use print_context to print the current context in terminal and print_thing to print something.
{% load debug %}
{% print_context %}
{% print_thing 'print this string' %}
You can create a template tag that will do anything a standard Python function can do. This is because a template tag essentially calls the function you create.
Use the constraints of the Django template system to your advantage to create well designed applications where the business logic is located in the views, models, and helpers, and not in the templates.
You may create a class which includes the type, so you can call the type like: variable.type or you can send the type data from the controller.
If you need to make reactive programming logic at the front end, I'd suggest you use Vue, React or Angular.
How to render a jinja template within a custom execution module?
I am trying to write a custom module to update confluence pages automatically. It is designed to be similar to "file.managed" call (only template source, context, and it has to respect pillar data available for given node).
can someone offer an example of pillar/context aware function call for rendering jinja template in custom module?
do the same as saltstack does in their file.managed
example :
if template:
contents = __salt__['file.apply_template_on_contents'](
contents,
template=template,
context=context,
defaults=defaults,
saltenv=__env__)
I have created a complex Advanced PDF / HTML Template using freemarker. When I make no changes and attempt to save the template, the template saves with no errors. When I add a new field to the template through the UI or even simple HTML "Test" and then try to save, I get the following error:
"Parse exception during template merging.
com.netledger.templates.TemplateServiceException: Parse exception
during template merging. freemarker.core.ParseException: Unexpected
end of file reached."
If I proceed and save anyway, the printed PDF gives the same error and displays:
"The template stored was invalid". What does this error mean and how can I resolve it?
It means that you have some FreeMarker construct in the template that weren't closed. Like, and ${ without the matching }, or an <#if ...> without the matching </#if>.
BTW, usually the error message also tells what wasn't closed. Perhaps you are using an old FreeMarker version where error message quality was lower. You may try to copy-paste the template into http://freemarker-online.kenshoo.com/ to see what a more recent version says.
I'm developing a Pyramid project with jinja2 templating engine. Following the jinja2 documentation I've find out a way to load different templates from a unique view. But taking into account that the module pyramid_jinja2 was already configured in my app with a default path for templates. I was wondering if there is another way more elegant to get this done. This is my approach:
from jinja2 import Environment, PackageLoader
#view_config(context=Test)
def test_view(request):
env = Environment(loader=PackageLoader('project_name', 'templates'))
template = env.get_template('section1/example1.jinja2')
return Response(template.render(data={'a':1,'b':2}))
Can I get an instance of the pyramid_jinja2 environment from somewhere so I don't have to set again the default path for templates in the view?
The following is enough:
from pyramid.renderers import render
template = "section/example1.jinja2"
context = dict(a=1, b=2)
body = render(template, context, request=request)
And to configure loading do in your __init__.py:
config.add_jinja2_search_path('project_name:templates', name='.jinja2', prepend=True)
We have a custom Jinja filter that we use for creating cache busting URL for our Javascript and CSS resources. We now noticed that in our production environment the final, compiled templates get cached. This results in a problem since our template filter sometimes doesn't create a new URL (i.e. when the template wasn't changed but the Javascript was).
Is there a way to force Jinja to reevaluate a certain filter every time and don't cache the result?
Edit 1: We are using constant inputs (name of the file) to the filter.
After lots of Googling, I finally found the real solution to this. Jinja has a special helper called contextfilter that you can use to decorate your function with to make your filter context-aware (and context-dependent). The Jinja bytecode cache will not cache this computed value, even when a constant is passed as input.
In your filter in Python:
from jinja2 import contextfilter
#contextfilter
def asset_url(context, url):
return some_url_thing(url)
In your template:
<link rel="stylesheet" href="{{ 'styles.css' | asset_url }}" />
There is a way to disable caching of the result of a particular filter: it's by not using a constant input, e.g. by exposing a random source as a global variable.
# Expose to Jinja
from random import random as RANDOM
And in the templates
{{ RANDOM() | eval_this_filter_every_time }}
The caching behaviour of Jinja2 can be configured using the cache_size setting: http://jinja.pocoo.org/docs/api/#jinja2.Environment
However, this only caches the templates itself. As long as the input for a filter is variable the output will be variable too.
So... how are you using the filter? Can you post the part of the template and the filter that is getting cached?
The easiest way to do this is:
On your flask server script do this:
from time import ctime
In your app.route() function,
in the return line add:
time=ctime()
for example:
return render_template('signup', error = error, time = ctime())
In the html reference for your css file(if using jinja2) add:
?{{time}}
at the end of your reference line.
That shoud look like this:
<link rel="stylesheet" href="{{ url_for('static', filename='css/base.css')}}?{{time}}">
Worked pretty good for me and the browser stopped caching.
I hope it helps!
Cheers!
-Nelio