[Fixed]-How django's template tag "include" work with None

1👍

{% if fragment_variable %}
    {% include fragment_variable %}
{% else %}
    <!-- something else -->
{% endif %}

Edit:

Since you are using django version prior to 1.8, take a look at the setting TEMPLATE_STRING_IF_INVALID, it sets the default value for invalid variables.

Also take a look at How invalid variables are handled:

Generally, if a variable doesn’t exist, the template system inserts
the value of the engine’s string_if_invalid configuration option,
which is set to ” (the empty string) by default.

This behavior is slightly different for the if, for and regroup
template tags. If an invalid variable is provided to one of these
template tags, the variable will be interpreted as None. Filters are
always applied to invalid variables within these template tags.

For that matter, I still consider using if is the best exercise.

Leave a comment