Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Do I really need apps.py inside an app?

Tags:

python

django

When creating an app with python manage.py startapp myapp, it automatically creates an apps.py file.

from django.apps import AppConfig


class MyappConfig(AppConfig):
    name = 'myapp'

When I removed it, everything seems to work as before (at least my tests all passed). Is it a bad practice to remove these kind of files from the apps? Should we keep them to avoid side effects?

like image 309
Julien Le Coupanec Avatar asked Sep 21 '17 14:09

Julien Le Coupanec


People also ask

What is purpose of apps py in Django?

Purpose of apps.py file: 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. From Application Configuration documentation: Application configuration objects store metadata for an application.

Can a Django project have multiple apps?

A Django project is simply a web application consisting of one or more apps within it.

What is difference between app and project in Django?

A project represents the entire website whereas, an app is basically a submodule of the project. A single project can contain multiple apps whereas, an app can also be used in different projects. A project is like a blueprint of the entire web application whereas, apps are the building blocks of an web application.


1 Answers

The recommended approach in Django is to use the app config in your INSTALLED_APPS:

INSTALLED_APPS = [
    'myapp.apps.MyappConfig',
    ...
]

If you do this, then the apps.py file is required.

However, if you do not customize the app config at all, then it is possible to remove the apps.py if you use the app name in INSTALLED_APPS instead:

INSTALLED_APPS = [
    'myapp',
    ...
]
like image 174
Alasdair Avatar answered Sep 19 '22 11:09

Alasdair