[Fixed]-Django/Jinja: "Unused is at end of expression"

1đź‘Ť

âś…

My guess it that this is happening because Django isn’t fully compatible with Jinja2. This is taken from the Jinja FAQ:

The default syntax of Jinja2 matches Django syntax in many ways. However this similarity doesn’t mean that you can use a Django template unmodified in Jinja2. For example filter arguments use a function call syntax rather than a colon to separate filter name and arguments. Additionally the extension interface in Jinja is fundamentally different from the Django one which means that your custom tags won’t work any longer.

Granted, I’m not sure if this is why it isn’t working for you.

However, the Django documentation suggests using the {% if %} template tag to check for definedness (definitely a word):

The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output

What is important to you here is the “i.e. exists”.

My best guess is that because of this, Jinja in Django does not use the defined function, because you are supposed to just use the {% if %} tag.

Please do note, however, that this is not the behavior in regular Python:

if variable:
    print(variable)

# NameError: name 'variable' is not defined 

0đź‘Ť

The error is because Django is treating your template as Django template language. Your jinja2 templates belong in your app’s jinja2 directory, e.g. /home/django/mediwiki/medisearch/jinja2/medisearch/response.html

👤Alasdair

Leave a comment