【发布时间】:2015-01-12 14:19:55
【问题描述】:
我想为当前应用获取一个 Django app_config 实例。
例如:
baseapp/views.py
class MyBaseView(RedirectView):
def get_redirect_url(self):
# That's the hypothetical method I would like to have
app_config = apps.get_current_app_config()
return app_config.url
app1/views.py
class MyView1(MyBaseView):
pass
app1/apps.py
class MyApp1Config(AppConfig):
name = 'myapp1'
verbose_name = 'My Application 1'
url = 'http://example.com/'
app2/views.py
class MyView2(MyBaseView):
pass
app2/apps.py
class MyApp2Config(AppConfig):
name = 'myapp2'
verbose_name = 'My Application 2'
url = 'http://example.org/'
基本上我希望有一个超类能够使用子类信息来执行某些操作。重定向只是一个示例,因为我实际上正在做更复杂的事情并从子类中读取更多数据。
【问题讨论】:
-
@DanielRoseman 是的,文档很清楚使用应用程序的名称来获取其 app_config。如果我想知道我目前所在班级的名称怎么办?