Django template variable type

Answered

So I have two different templates, both with same issues:

#1:

$("#{{ field.uuid }}").toggle({{ condition }}());

#2:

function cond() {
{% if matching_values_is_list %}
var vals = ({{ matching_values }});
return vals.indexOf({{ field_value }}) > -1;
{% else %}
return ({{ matching_values }}) == {{ field_value }};
{% endif %}
}

 

Both clearly fail to understand (and they rightly can't) the type of template variable. First actually uses the second in the above examples.

The first one complains about "expression expected", expecting some context about {{condition}} variable.

The second complains about unreachable code for the second return statement as if matching_values_is_list were a constant true statement. Additionally, if I use JS if statement instead of django template if statement, the entire template just goes beserk, parser breaking in the middle of variable "matching_values" name.

 

Is there any way I can tell pycharm's parser what those variables actually are so that it wouldn't complain about their usage?

1 comment
Comment actions Permalink
Official comment

Hello.

PyCharm does not offer it for now, but there is a feature request, please vote for it: https://youtrack.jetbrains.com/issue/PY-18759

It is generally bad idea to use Django as JS preprocessor with PyCharm. A lot of issues been reported about that. It is always better to generate JSON:

 

def spam(request):
return render(request, "foo.html", {"json": serialize('json', data)})
<script>
var data = JSON.parse('{{ json|escapejs }}')
</script>

Please sign in to leave a comment.