Reorganization of the Django project: cannot import the application

I am trying to restructure my project as recommended in the last two Django magazines (for Django 1.11).

After the restructuring is complete, my application cannot be imported.

secureDash_project
    β”œβ”€β”€ README.rst
    β”œβ”€β”€ config
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ __pycache__
    β”‚   β”œβ”€β”€ db.sqlite3
    β”‚   β”œβ”€β”€ settings
    β”‚   β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”‚   β”œβ”€β”€ __pycache__
    β”‚   β”‚   └── settings.py
    β”‚   β”œβ”€β”€ urls.py
    β”‚   └── wsgi.py
    β”œβ”€β”€ db.sqlite3
    β”œβ”€β”€ manage.py
    β”œβ”€β”€ requirements.txt
    └── secureDash
        β”œβ”€β”€ __init__.py
        β”œβ”€β”€ __pycache__
        β”œβ”€β”€ dash
        β”‚   β”œβ”€β”€ __init__.py
        β”‚   β”œβ”€β”€ __pycache__
        β”‚   β”œβ”€β”€ admin.py
        β”‚   β”œβ”€β”€ apps.py
        β”‚   β”œβ”€β”€ forms.py
        β”‚   β”œβ”€β”€ migrations
        β”‚   β”‚   └── __init__.py
        β”‚   β”œβ”€β”€ models.py
        β”‚   β”œβ”€β”€ tests.py
        β”‚   β”œβ”€β”€ urls.py
        β”‚   └── views.py
        └── templates
            └── dash

settings.py excerpt:

...
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))))
ENV_PATH = os.path.abspath(os.path.dirname(__file__))

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Application definition

INSTALLED_APPS = [
    'secureDash.dash.apps.DashConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 'django_static_jquery',
    # 'bootstrap3',
] ...

At startup

python3 manage.py runserver --settings=config.settings.settings

I get:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x1076b0d08>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.5/site-packages/django/apps/config.py", line 142, in create
    app_module = import_module(app_name)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'dash'

secureDash.dash.apps.DashConfig:

from django.apps import AppConfig


class DashConfig(AppConfig):
    name = 'dash'
0
source share
1 answer

Replacing secureDash.dash.apps.DashConfigwith secureDash.dashin INSTALLED_APPSshould fix it.

+2
source

Source: https://habr.com/ru/post/1685531/


All Articles