gpt4 book ai didi

python - 设置文件中的 Django 应用程序发现问题。怎么解决?

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:37 24 4
gpt4 key购买 nike

我正在使用 django 1.7.8 构建一个项目

为了为本地和生产环境制作不同的设置文件,我对设置文件进行了更改。

我添加了 local_settings.py 和 Production_settings.py,它们是另一个名为 settings_template.py (所有设置变量的基础)的文件的精确副本

因此我的 settings.py 文件如下所示:

####################################################################################################
#1) Never commit this file
#2) use local_settings.py for local use
#3) commit settings.template if you want to commit any addition or removal in actual settings which
# should reflect on production as well .
###################################################################################################

# lowest priority of base template for setting , import it first , then local settings can override its values
# LOWEST PRIORITY
try:
from settings_template import *
except ImportError as e:
pass


# if local or development version of settings.py file is present use it
# MID PRIORITY
try:
from local_settings import *
except ImportError as e:
pass

# if production version of settings.py file is present use it | HIGHEST PRIORITY
# HIGHEST PRIORITY
try:
from production_settings import *
except ImportError as e:
pass

我的settings_template.py文件看起来像:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os


MY_SITE_URL = "http://127.0.0.1:8000"

BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'j2x^8(w64ayq@jr5bc5gfdgdfgfdgfdgdfgdfgfdgfg3+z7'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',

# for all auth
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',

# our application apps
'users',
'app_setings',
'customer_service',
)

MIDDLEWARE_CLASSES = (
'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 = 'testproject.urls'

WSGI_APPLICATION = 'testproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

#DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
#}

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testproject',
'USER': 'django',
'PASSWORD': 'test',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

# use utc time all over
#USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = BASE_DIR + "/static/media/uploads/"

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = MY_SITE_URL + '/static/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"

#STATIC_ROOT = BASE_DIR + '/static/'
STATIC_URL = '/static/'

# added for all auth
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)


TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request',
'allauth.account.context_processors.account',
'allauth.socialaccount.context_processors.socialaccount',
)

SITE_ID = 1
LOGIN_REDIRECT_URL = '/'


######### django all auth #########################################################
SOCIALACCOUNT_PROVIDERS = {
'facebook': {
'SCOPE': ['email'],
'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
'METHOD': 'oauth2',
'VERIFIED_EMAIL': False
}
}
###################################################################################


################ Custom all auth settings ################################
SOCIALACCOUNT_QUERY_EMAIL = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION = "none"
###################################################################################


###################################################################################
# custom variables for social AUTH
# store social app ID here
FACEBOOK_WEB_ID = 1
FACEBOOK_MOB_ID = 1
GOOGLE_WEB_ID = 3
GOOGLE_MOB_ID = 4
###################################################################################


# Additional locations of static files
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)



# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)


TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

我之前添加了一个名为'users'的应用程序,在注册模型后也可以在 django 管理面板中轻松看到该应用程序。

现在,当我尝试添加 2 个名为 'customer_service''app_settings' 的新应用并运行命令时:

python manage.py makemigrations customer_service

我在终端中收到此错误:

App 'customer_service' could not be found. Is it in INSTALLED_APPS?

因此,当我将 settings_template.py 的全部内容复制到 settings.py 文件并运行 makemigrations 时,它起作用了

然后在进行迁移后,我再次恢复了如上所示的settings.py,并且代码工作正常。

但是当我在各自的 admin.py 文件中添加 customer_service 和 app_settings 应用程序的模型并启动服务器时,我无法在 django 管理面板中看到它们的模型

当我再次将 settings_template.py 的所有内容复制到 settings.py 文件并重新启动服务器时它起作用了,我可以在 django admin 中看到所有模型。

<小时/>

How can i solve this ? I want to have a basic setup for local and production but it should work normally as well .. like i am able to use and create and migrate django apps smoothly .

<小时/><小时/>

编辑:在上述情况下,我没有将 local_settings.py 或 production_settings.py 文件放置在设置文件夹中。所以它们不会被考虑,只有 settings_template 在这里有任何影响。

最佳答案

settings_template 导入后,您将从 local_settingsproduct_settings 导入 *。因此您的 INSTALLED_APPS 正在某处被覆盖。

环境分离的想法是将通用设置放在一个文件中(在您的情况下为settings_template)。然后仅在 local_settingsproduct_settings 中添加环境特定设置。

此外,您可能不想将生产设置和本地设置直接导入到设置中。当您在本地环境中时,您应该从本地文件导入;当您在生产环境中时,您应该从生产文件导入。

最好将所有内容放入 settings.py 中,然后从本地或生产环境导入,而不是像您那样创建 3 个文件。

关于python - 设置文件中的 Django 应用程序发现问题。怎么解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34234918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com