【问题标题】:Is there a way to get the app_config for the current app?有没有办法获取当前应用程序的 app_config ?
【发布时间】: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。如果我想知道我目前所在班级的名称怎么办?

标签: python django


【解决方案1】:

我只需要做类似的事情和以下工作;

from .apps import MyApp1Config
from django.apps import apps 

app_config = apps.get_app_config(MyApp1Config.name)

【讨论】:

    猜你喜欢
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多