[Django]-Determining if I need quotes in HTML with django template

7👍

✅

Don’t do this. You are manually trying to replicate a JS data structure, when there already exists a well-defined structure that both Python and JS know about, namely JSON. Use that instead. Convert the dict to JSON in your view, and pass it to the template where it can be output directly.

0👍

You could define a filter called “check_v_type” to call in your template as follows:

{% if v|check_v_type == 'str' %}

But I don’t think that makes for good logic. Generally, you want to avoid function calls in your templates. Instead, in your view, you could create a dictionary of tuples of the form:

d = {'key1': (.98, False), 'key2': ('some_str', True), #...}

You could then use the boolean in each tuple to decide how to format the value in your HTML.

Leave a comment