Home > Back-end >  Django Project Directory Structure
Django Project Directory Structure

Time:05-16

in my django project structure, i want all my django apps in a separate Apps Folder, but when i include it in settings.py it raises an error,

raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Cannot import 'TestApp'. Check that 'Apps.TestApp.apps.TestappConfig.name' is correct.

INSTALLED_APPS = [
    ...

    'Apps.TestApp'
]

But

when i only include TestApp, i raises no module named 'TestApp' Error.

INSTALLED_APPS = [
    ...

    'TestApp'
]

CodePudding user response:

If you are using django version < or = 2 then you should register your app like

INSTALLED_APPS = [
   ...
 'testapp.apps.TestappConfig' 
]

the app name should not be in 'UPPER' case otherwise you will get errors.

if you are using django > or = 3 then you can register your app with it's original name too.

You are registering your app in 'Title' style which is not permitted.

CodePudding user response:

You could do the following in your settings.py file:

INSTALLED_APPS = [
  ... # other necessary apps here
  
  # include your local apps you are creating for your project here
  'testapp.apps.app_name', # assuming app_name is one of your apps
  'testapp.apps.another_app',
  'testapp.apps.third_custom_app'

]

Then in each of your app folders (where your models.py, views.py, urls.py, etc. are) include an apps.py file that follows the following pattern:

from django.apps import AppConfig


class AppNameConfig(AppConfig): # note the formatting of this class name
    default_auto_field = "django.db.models.BigAutoField"
    name = "apps.app_name" # apps is the name of the directory where all your apps are located. 
                           # app_name is the name of the individual directory within your apps directory where this apps.py file is saved
  • Related