Django template variable type


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


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


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
Official comment


PyCharm does not offer it for now, but there is a feature request, please vote for it:

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)})
var data = JSON.parse('{{ json|escapejs }}')

Please sign in to leave a comment.