apps.py 在 Django 中是一个用于配置应用程序的 Python 文件。

huangapple go评论92阅读模式
英文:

What is apps.py in django?

问题

我阅读了关于这个的信息,但我感到困惑。

应用服务器获取已安装的应用程序并为每个应用程序加载或生成一个AppConfig对象。然后,将这些AppConfig对象的集合存储在一个名为theApps的类的实例中。

请有人用简单的示例来解释一下。
谢谢。

英文:

I read about this,And i cunfused

> The application server takes the INSTALLED_APPS and loads or
> generates an AppConfig object for each app. This collection of
> AppConfig objects are then stored in an instance of theApps class.

Please someone explain with simple example.
Thanks

答案1

得分: 2

我希望这不会让你更加困惑。但我会尽力。

假设你运行了 python manage.py startapp hello,Django 会默认为你创建目录/文件,包括 apps.py。

但是为了使用 hello 应用程序,你必须将其添加到项目设置中的 INSTALLED_APPS 中。

当你运行 python3 manage.py runserver 时,它会执行以下操作(总结如下):

  • 首先加载你的项目的 settings.py。
  • 然后转到每个 INSTALLED_APPS
  • 为每个已安装的应用程序创建一个 'app 类'。如果存在 apps.py,则默认使用其中的内容。否则,它将只加载基本的 AppConfig。

^ 这回答了:应用服务器获取 INSTALLED_APPS 并为每个应用加载或生成一个 AppConfig 对象

对于这个上下文:这些 AppConfig 对象的集合然后存储在一个 TheApps 类的实例中

在我的上面的示例中,它创建了一个 class helloConfig(AppConfig)。helloConfig 类继承自 AppConfig。

如果我要创建一个实例,它将如下所示。

myinstance = helloConfig()
myinstance # -> 在 `myinstance` 中存储了一组 AppConfig 对象

想象一下 Django 为每个已安装的应用程序执行这个操作,考虑到应用程序的名称。

额外的一点说明,不是每个应用程序都需要有一个 apps.py,但是 AppConfig 具有可能有用的其他属性。如果你想要利用这些属性,Django 会创建一个继承自 AppConfig 的类包装器(class helloConfig)。

这样你就可以使用 def ready(),这是 AppConfig 中的一个属性(而不是直接编辑 AppConfig 的 ready 方法)。

class HelloConfig(AppConfig):
    ...

    def ready():
       # 当 Hello 应用程序准备就绪时运行此操作
英文:

I hope this doesn't confuse you more. But I'll do my best.

Let's say you ran python manage.py startapp hello django creates directories/files for you by default including the apps.py.

But in order to use the hello app you have to add it in INSTALLED_APPS project settings.

When you ran python3 manage.py runserver it does this (in summary):

  • first loads your project's settings.py
  • Then goes to each of the INSTALLED_APPS
  • creates an 'app class' for each of the installed apps. It will use what's in apps.py by default if it exists. Otherwise, it will just load the base AppConfig.

^ That answers: The application server takes the INSTALLED_APPS and loads or generates an AppConfig object for each app

For this context: This collection of AppConfig objects are then stored in an instance of theApps class.

In my example above, it created a class helloConfig(AppConfig). The class helloConfig inherited from AppConfig.

If I were to create an instance it will looks like this.

myinstance = helloConfig()
myinstance #-> `collection of AppConfig objects are then stored` in the `myinstance`

Imagine Django doing that for each of the Installed app with the respect to the apps' name.

Additional side note, It's not required for the every apps to have an apps.py but AppConfig has other attributes that may be useful. If you want to utilize those attributes, django creates a class wrapper (class helloConfig) that inherits from the AppConfig.

This way you can use def ready() which is one of the attributes in AppConfig (without editing AppConfig's ready method directly)

class HelloConfig(AppConfig):
    ...

    def ready():
       # Run this when the Hello app is ready

答案2

得分: 0

这个文件是为了帮助用户包含应用程序的任何配置而创建的。使用这个文件,您可以配置应用程序的一些属性。

英文:

This file is created to help the user include any application configuration for the app. Using this, you can configure some of the attributes of the application.

答案3

得分: 0

这是一个配置文件。您可以定义元数据和设置。如果需要,还可以覆盖ready()方法。

英文:

It's a configuration file. You can define metadata and settings. You can also override the ready() method if needed.

huangapple
  • 本文由 发表于 2023年8月10日 11:44:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定