Alex Rivera | Logout

How to get current application in Django

Asked 2010-11-23T13:08:36.067
11

I'm using the Django 1.3 alpha to create a project with two applications. I use 1.3 because of the class-based views. For these two applications I have a set of common base view-classes, that are inherited by the actual views in the applications. In the base class, is there a way to find out what application the view is "called" from? E.g. can I use the URL to get the "current" application?

Edit
Report

1 Answer

8

If you are inheriting from the generic list and detail views that django provides you could access self.model to gain access to the model that the view displays information about, otherwise you will probably have use django's resolve(): resolve(self.request.path).

You also could make your own View subclass that you call with a keyword of your choice:

# views.py
from django.views.generic.base import View
class MyView(View):
     app_name = None

# urls.py
from django.conf.urls.defaults import *
from some_app.views import MyView

urlpatterns = patterns('',
    (r'^myview/', MyView.as_view(app_name='app_name')),
)

Then you should be able to access it through self.app_name.

answered 2010-11-23T13:45:15.400

Your Answer