Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adapt a view if an app is installed with Django

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?

like image 630
nlassaux Avatar asked Jul 17 '12 12:07

nlassaux


People also ask

How do I see installed apps in Django?

The list of installed applications is defined in settings. INSTALLED_APPS . It contains a tuple of strings, so you can iterate on it to access each application's name.

What file contains the list of installed apps in a Django project?

INSTALLED_APPS. Within the newly created settings.py file is a configuration called INSTALLED_APPS which is a list of Django apps within a project.

How will you import views in Django?

from django.shortcuts import render # Create your views here. Find it and open it, and replace the content with this: members/views.py : from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("Hello world!")


2 Answers

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.

like image 186
Eugene Yarmash Avatar answered Oct 09 '22 10:10

Eugene Yarmash


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 %}
like image 32
scytale Avatar answered Oct 09 '22 10:10

scytale