Reputation: 2406
I have a web app with a project that works alone (it's index, login.. pages).
I would need to change the index page if a new app is installed (e.g: add a link, a table in the template with my app models..). Have it dynamic.
The removal of the app must let the project intact and just remove the link.
How can I do that? Is it possible?
Upvotes: 6
Views: 5361
Reputation: 590
simple_tag
version:
The tricky part is that you can't pass arguments to your simple_tag
when it's used in a conditional. Therefor, you create a new variable specifically for your installed app with as is_myapp_installed
.
In templatetags/my_filters.py
:
from django import template
register = template.Library()
@register.simple_tag
def is_app_installed(app):
from django.apps import apps
return apps.is_installed(app)
In template:
{% load my_filters %}
...
{% is_app_installed "myapp" as is_myapp_installed %}
{% if is_myapp_installed %}
...
{% endif %}
Upvotes: 1
Reputation: 149963
You can use the Django's application registry:
In [1]: from django.apps import apps
In [2]: apps.is_installed("django.contrib.admin")
Out[2]: True
An application can actually be enabled by using a dotted Python path to either its package or the application's configuration class (preferred). Simply checking if "app_name"
is in settings.INSTALLED_APPS
will fail in the latter case.
Upvotes: 17
Reputation: 935
Or use a custom context processor.
In installed_apps.py
from django.conf import settings
def installed_apps(request):
return {
'app_installed' : 'app_name' in settings.INSTALLED_APPS
}
In settings.py
:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'installed_apps.installed_apps'
)
Upvotes: 5
Reputation: 12641
def my_view(request):
from django.conf import settings
app_installed = 'app_name' in settings.INSTALLED_APPS
return render_to_response(template_name, {'app_installed': app_installed})
template:
{% if app_installed %}
...
{% endif %}
Upvotes: 5