If you are in the view and want to retrieve the app name using Python ( the app name will be used for further logic ), how would you do it ?
You could do:
from django.core.urlresolvers import resolve
....
resolve(request.path).app_name
See How to get current application in Django and resolve()
EDIT: You can now use request.resolver_match.app_name which avoids resolving a second time and avoids an import. Do it this way:
request.resolver_match.app_name
resolve(request.path).app_name
- in python3/django2 it's from django.urls import resolve
. And both of your answers doesn't work. –
Polik resolver_match
isn't available in middleware –
Peyton __package__
or
__package__.rsplit('.', 1)[-1]
should be the easiest way. Second converts a.b.c
to c
.
__package__
returns 'project.app_name.folder' __package__.rsplit('.', 1)[-1]
returns 'folder' –
Protero Another way to do it is get the current object or use self. see bellow
obj.__module__.split('.')
This will return a list with the object name split up by the '.'
You can get application name from model: Book._meta.app_label
.
I've found in django/contrib/admin/widgets.py
:
class RelatedFieldWidgetWrapper(forms.Widget):
...
def get_context(self, name, value, attrs):
from django.contrib.admin.views.main import IS_POPUP_VAR, TO_FIELD_VAR
rel_opts = self.rel.model._meta
info = (rel_opts.app_label, rel_opts.model_name)
...
...
so to sum up, If you're looking for the app_name
of where your code is written, then use this:
app_name = __package__
However, if you need the app_name
of where the object is being called (which might be in an app other than coding app), then use this:
import sys
from django.urls import resolve
app_name = sys.modules[resolve(request.path_info).func.__module__].__package__
maybe this can help you..
from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(self.__class__)
url = urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label,
content_type.model), args=(self.id,))
url will return you all adress and you can parse it for your app and model...
This solution not very simple, but it works
import sys
sys.modules[resolve(request.path_info).func.__module__].__package__
You can use context processors. To create context processor for an app name do the following:
Say you have an app named myapp. Inside myapp directory create a context_processor.py
In context_processor.py define following method:
def get_common_context(request): return { 'app_name':'myapp' }
Add your context processor to TEMPLATES in settings.py
TEMPLATES = [ { ... 'OPTIONS': { 'context_processors': [ ... 'myapp.context_processor.get_common_context', ], }, }, ]
Access app_name in your template.
{{ app_name }}
Access app_name in your view using RequestContext
I believe the updated solution is view.__module__
. This returns your app_name
both from Django and Django Rest Framework.
My scenario was working with dynamically module
or app_name
from view call so that I can work with access permission check for that particular module.
maybe my option would work for someone:
### apps.py
from django.apps import AppConfig
class MyApp(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'my_app'
### models.py
from .apps import MyApp
name = MyApp.name
© 2022 - 2025 — McMap. All rights reserved.
app_name = __package__.split('.')[0]
– Koralle