How to get an app name using python in django
Asked Answered
U

10

35

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 ?

Unprofitable answered 14/5, 2011 at 6:32 Comment(1)
app_name = __package__.split('.')[0]Koralle
T
33

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
Tiro answered 14/5, 2011 at 6:36 Comment(3)
resolve(request.path).app_name - in python3/django2 it's from django.urls import resolve. And both of your answers doesn't work.Polik
@Tiro I want to get the app_label instead of app_name. Is there any method to get app_label inside views.py. Or some way to get the app_label using app_name ?Volgograd
resolver_match isn't available in middlewarePeyton
P
9
__package__

or

__package__.rsplit('.', 1)[-1]

should be the easiest way. Second converts a.b.c to c.

Polik answered 3/3, 2018 at 16:22 Comment(1)
__package__ returns 'project.app_name.folder' __package__.rsplit('.', 1)[-1] returns 'folder'Protero
R
7

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

Rayleigh answered 15/12, 2014 at 7:25 Comment(1)
Sorry, what is obj?Polik
G
7

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)
        ...
    ...
Glori answered 9/2, 2018 at 13:15 Comment(0)
V
4

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__
Vintager answered 26/4, 2020 at 13:44 Comment(0)
C
3

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...

Crammer answered 9/7, 2012 at 12:56 Comment(0)
B
2

This solution not very simple, but it works

import sys

sys.modules[resolve(request.path_info).func.__module__].__package__
Buchan answered 26/8, 2019 at 14:38 Comment(0)
B
0

You can use context processors. To create context processor for an app name do the following:

  1. Say you have an app named myapp. Inside myapp directory create a context_processor.py

  2. In context_processor.py define following method:

     def get_common_context(request):
         return {
             'app_name':'myapp'
         }
    
  3. Add your context processor to TEMPLATES in settings.py

     TEMPLATES = [
         {
             ...
             'OPTIONS': {
                 'context_processors': [
                     ...
                     'myapp.context_processor.get_common_context',
                 ],
             },
         },
     ]
    
  4. Access app_name in your template.

     {{ app_name }}
    
  5. Access app_name in your view using RequestContext

Burleigh answered 19/8, 2021 at 7:23 Comment(2)
This is unnecessary given that python offers this out of the box.Motionless
Could you share how this can be done?Burleigh
P
0

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.

Porthole answered 26/8, 2021 at 7:24 Comment(0)
E
0

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
Extravasate answered 27/1, 2024 at 17:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.