Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an app name using python in django

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 ?

like image 481
Zach Avatar asked May 14 '11 06:05

Zach


2 Answers

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 
like image 109
Henry Avatar answered Sep 21 '22 14:09

Henry


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)         ...     ... 
like image 34
simno Avatar answered Sep 21 '22 14:09

simno