2017-04-25 57 views
0

我試圖重構我的項目,以匹配Django最新的兩本書(適用於Django 1.11)中的建議。Django項目重組:無法導入應用程序

完成重組後,我的應用程序無法導入。

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摘錄:

... 
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', 
] ... 

當運行

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

我得到:

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

您是否嘗試用'secureDash.dash'替換'secureDash.dash.apps.DashConfig'? –

+0

沒有。這工作雖然。你能幫我理解爲什麼這有效嗎? – brewcrazy

+1

'django'尋找你的'apps'根目錄。所以在這種情況下,它是'secureDash/dash /'文件夾。之後,它會查找'views.py'和'urls.py'來配置它。 –

回答

2

替換secureDash.dash.apps.DashConfigsecureDash.dashINSTALLED_APPS應該修復它。

相關問題