我正在尝试将Visual Studio用于我的Django项目。我的Visual Studio版本是2017,我的Python版本是3.6,而我的Django版本开头是1.1.18。 然后将其升级到2.1.5。我制作一个应用程序,然后将其添加到已安装的应用程序中。但是,当我迁移项目时,出现以下错误:
Traceback (most recent call last):
File "E:\Django_Try\Test4\Test4\manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "E:\Django_Try\Test4\Test4\env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "E:\Django_Try\Test4\Test4\env\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "E:\Django_Try\Test4\Test4\env\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "E:\Django_Try\Test4\Test4\env\lib\site-packages\django\apps\registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "E:\Django_Try\Test4\Test4\env\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'saeed'
The interactive Python process has exited.
The interactive Python process has exited.
>>>
似乎无法识别我的应用。
我的设置:
"""
Django settings for Test4 project.
Generated by 'django-admin startproject' using Django 1.9.1.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""
import os
import posixpath
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '79c97724-a0e9-44f0-bffd-e255d60e5460'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
# Add your apps here to enable them
'saeed',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
#'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Test4.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Test4.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static']))
似乎无法识别我的应用。
请通知我。
赛义德
答案 0 :(得分:0)
编辑:您似乎已将主/基本应用(包含settings.py
的应用)从Test4
重命名为saeed
。您无需在Django项目中注册基础应用程序,因为它已经是该程序的入口点。看来您的settings.py
文件的其余部分是为名为Test4
而不是saeed
的基本应用程序配置的(例如,由于ROOT_URLCONF = 'Test4.urls'
可以看出来)。您可以在Django here中阅读有关项目和应用程序的更多信息。
因为通常的做法是让基本应用程序与项目共享相同的名称,所以建议将saeed
目录(返回)的名称更改为Test4
。
如果要创建新应用,可以运行:
python manage.py startapp saeed
因此您的项目文件结构将如下所示:
Test4/ # project directory
saeed/ # your app
migrations/
models.py
apps.py
Test4/ # base app for the site
settings.py
urls.py
manage.py
requirements.txt
然后将模型放入saeed
而不是Test4
中。因此,您的项目目录Test4/
将包含Test4/
,saeed/
和manage.py
等。创建此新应用后,将可以进行以下操作:
原始答案:
确保saeed/apps.py
的配置正确,例如:
# saeed/apps.py
from django.apps import AppConfig
class SaeedConfig(AppConfig):
name = "saeed"
verbose_name = "Saeed"
然后,您需要将INSTALLED_APPS
中的行从"saeed"
更改为
"saeed.apps.SaeedConfig"
有关更多信息,请参见docs on configuring applications。
答案 1 :(得分:0)
我认为这是不正确的文件位置层次结构。
例如应该如此。
project
mysite
settings.py
...
saeed
makemigrations
models.py
apps.py
...
manage.py