Adapt a view if an app is installed with Django

django installed apps
django installed_apps
django get app name
django apps folder
django apps examples
django multiple apps
django documentation
best django apps

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?

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 %}

Applications | Django documentation, A public site that lets people view polls and vote in them. If Django is installed, you should see the version of your installation. If you are still using Python 2.7, you will need to adjust the code samples slightly, as described in comments. The application registry is initialized in three stages. At each stage, Django processes all applications in the order of INSTALLED_APPS. First Django imports each item in INSTALLED_APPS. If it’s an application configuration class, Django imports the root package of the application, defined by its name attribute. If it’s a Python package, Django creates a default application configuration.

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.

Writing your first Django app, part 1 | Django documentation, Views (but not templates!) also incorporate some application logic, and all that Installing. the. Django. Framework. I recommend that you download and use the If you are going to use a version other than 1.6, read the release notes for any Usually there are clear instructions provided on how to adapt your code to the  – customers/apps.py: declares CustomersConfig class (subclass of the django.apps.AppConfig) that represents our Django app and its configuration. – gkzRestApi/settings.py: configures settings for the Django project, including INSTALLED_APPS list with Django REST framework and Customers Application.

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'
)

Pro Python System Administration, Source code for django.apps.registry Other registries must # provide a list of installed apps and are populated immediately. if installed_apps is None and app_config app_config.apps = self # Check for duplicate app names. counts  This is the simplest view possible in Django. To call the view, we need to map it to a URL - and for this we need a URLconf. To create a URLconf in the polls directory, create a file called urls.py. Your app directory should now look like:

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 %}

django.apps.registry | Django documentation, This view inherits methods and attributes from the following views: If you're using pagination, you can adapt the example template from the pagination docs. Using the Django authentication system¶. This document explains the usage of Django’s authentication system in its default configuration. This configuration has evolved to serve the most common project needs, handling a reasonably wide range of tasks, and has a careful implementation of passwords and permissions.

Generic display views | Django documentation, Web Application Development and Deployment with Python Daniel Rubio in view method from django.http import HttpResponsePermanentRedirect from the resolved url names cannot adapt dynamically to the different app instances,  URLs and views. A clean, elegant URL scheme is an important detail in a high-quality Web application. Django encourages beautiful URL design and doesn’t put any cruft in URLs, like .php or .asp. To design URLs for an application, you create a Python module called a URLconf. Like a table of contents for your app,

Beginning Django: Web Application Development and Deployment with , The get_response callable provided by Django might be the actual view (if this A Django installation doesn't require any middleware — MIDDLEWARE can be they must test for streaming responses and adjust their behavior accordingly: if  Sure, it is quite possible to run the Django server, then go to your browser, and type the entire URL in the address bar, but with PyCharm there is an easier way: use the pre-configured Django server run configuration with some slight modifications.

Middleware | Django documentation, Normally, when you write a Django View—the function that actually responds to an However, there is a helper function, django. template. loader. select template, If you're ever Writing an application where you need to do the same, keep admin application has to adapt itself to provide appropriate forms for any data  When you supply None as a value for an app, Django will consider the app as an app without migrations regardless of an existing migrations submodule. This can be used, for example, in a test settings file to skip migrations while testing (tables will still be created for the apps’ models).

Comments
  • This will fail if the application was enabled using an application configuration class. It's better to use the application registry.