Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current application in Django

Tags:

django

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?

like image 543
Some programmer dude Avatar asked Nov 23 '10 13:11

Some programmer dude


1 Answers

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.

like image 149
Bernhard Vallant Avatar answered Sep 28 '22 10:09

Bernhard Vallant