Jinja Namespace Object Undefined Error in Print Format Customised HTML Code

Hi,

I am facing this problem of trying to program in Jinja to customise print format. I tried to create a lot of variables using the Namespace scoping so that the variables can be used outside a loop.

For example, I tried the following code to insert as customised code in HTML to Print Format:

{% set det = namespace(abbr=‘’) %}
{% set det = namespace(DATE=‘01-01-2021’) %}
{% set det = namespace(BA=0) %}
{% set det = namespace(BO=0) %}
{% set det = namespace(CO=0) %}
{% set det = namespace(CR=0) %}
{% set det = namespace(CRAW=0) %}
{% set det = namespace(PH=0) %}
{% set det = namespace(CE=0) %}
{% set det = namespace(CEAW=0) %}
{% set det = namespace(SL=0) %}
{% set det = namespace(RL=0) %}
{% set det = namespace(CPFE=0) %}
{% set det = namespace(CPFR=0) %}
{% set det = namespace(CPFT=0) %}
{% set det = namespace(TP=0) %}
{% set det = namespace(NS=0) %}
{% set det = namespace(count=0) %}
{% set det = namespace(TA=0) %}
{% set det.count = det.count + 1 %}

However, I get the following error:

self.environment.handle_exception()
File “env/lib/python3.8/site-packages/jinja2/environment.py”, line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File “”, line 23, in top-level template code
jinja2.exceptions.UndefinedError: ‘jinja2.utils.Namespace object’ has no attribute ‘count’

I am rather confused as det.count was defined as ‘0’. I think it could be that I have created too many variables under namespace that created this error. But I have tried to read up on this issue but could not find answers.

I am hoping if someone can give me some clue on where I have gone wrong. I know I maybe can this better by doing some Python scripts rather than just doing it in Jinja. But I wanted to do something simply and if this works then it would be great for me.

Hope for any advise from anyone. Thanks in advance!

OK, I have found my own solution. My understanding of namespace was wrong.

What I should have done is to assign multiple variables into the namespace separated by comma like so:

{% set det = namespace(abbr=‘’, BA=0) %}

Hope this helps someone. Cheers!