开发者

How to get current application in Django

开发者 https://www.devze.com 2023-01-27 01:50 出处:网络
I\'m us开发者_开发知识库ing 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-clas

I'm us开发者_开发知识库ing 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号