如何在Django中获取当前的应用程序 [英] How to get current application in Django
问题描述
我正在使用Django 1.3 alpha来创建一个包含两个应用程序的项目。我使用1.3,因为基于类的视图。对于这两个应用程序,我有一组常见的基本视图类,它们由应用程序中的实际视图继承。在基类中,有没有办法找出哪个应用程序的视图是被叫的?例如。可以使用URL获取当前应用程序吗?
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?
推荐答案
如果您从通用列表和详细信息视图继承django提供您可以访问 self.model
以访问视图显示信息的模型,否则您可能会使用 django的resolve(): resolve(self.request.path)
。
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')),
)
然后你开始我们可以通过 self.app_name
访问它。
Then you should be able to access it through self.app_name
.
这篇关于如何在Django中获取当前的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!