[Answer]-Django cross-referencing application templates

1👍

You can create the template named app/app2_inc.html with the following content:

{% if "app2" in INSTALLED_APPS %}
    {% include app2_tpl %}
{% endif %}

And the include it with the with parameter:

{% include "app1/app2_inc.html" with app2_tpl="app2/path/to/template.html" %}

As alternative option you can create custom template tag:

from django.conf import settings
from django.template.loader import render_to_string

@register.simple_tag(takes_context=True)
def include_app2(context, template_name):
    if 'app2' in settings.INSTALLED_APPS:
        return render_to_string(template_name, context)
    return ''

And then call it from your app1 templates:

{% load my_tags %}

{% include_app2 "app2/path/to/template.html" %}

UPDATE: If you want to silently ignore the case if the template does not exist then change the template tag to:

from django.template import TemplateDoesNotExist
from django.template.loader import render_to_string

@register.simple_tag(takes_context=True)
def include_if_exist(context, template_name):
    try:
        return render_to_string(template_name, context)
    except TemplateDoesNotExist:
        return ''

So now you can safely include any template from any app. If this template is not available then template tag will just ignore it:

{% load my_tags %}

{% include_if_exist "app2/path/to/template.html" %}

Leave a comment