django settings 최적 구성
27336 단어 django
# encoding=utf-8
import os
import socket
SITE_ID = 1
#
#
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
#
# INSTALLED_APPS
from apps.kuser.mysetting import myapp as kuser_app
from apps.blog.mysetting import myapp as blog_app
MY_APPS = blog_app + kuser_app
#
from apps.blog.mysetting import my_staticfiles as blog_staticfiles
from apps.kuser.mysetting import my_staticfiles as kuser_staticfiles
MY_STATIC_DIRS = blog_staticfiles + kuser_staticfiles
#
from apps.blog.mysetting import my_templates as blog_templates
from apps.kuser.mysetting import my_templates as kuser_templates
MY_TEMPLATE_DIRS = blog_templates + kuser_templates
#
#
# , ,
try:
SECRET_KEY = os.environ['SECRET_KEY']
except:
try:
with open(os.path.join(PROJECT_ROOT, 'db/secret_key').replace('\\', '/')) as f:
SECRET_KEY = f.read().strip()
except:
SECRET_KEY = '*lk^6@0l0(iulgar$j)faff&^(^u+qk3j73d18@&+ur^xuTxY'
#
def hostname():
sys = os.name
if sys == 'nt':
hostname = os.getenv('computername')
return hostname
elif sys == 'posix':
host = os.popen('echo $HOSTNAME')
try:
hostname = host.read()
return hostname
finally:
host.close()
else:
raise RuntimeError('Unkwon hostname')
#
# ,
#ALLOWED_HOSTS
if socket.gethostname().lower() == hostname().lower():
DEBUG = TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
else:
ALLOWED_HOSTS = [
'baidu.com',
'0.0.0.0',
]
DEBUG = TEMPLATE_DEBUG = False
#
MYDB = {
'mysql': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'books', #
'USER': 'root', #
'PASSWORD': '', #
'HOST': '', # , localhost
'PORT': '3306', #
},
'sqlite': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_ROOT, 'db/db.sqlite3').replace('\\', '/'),
}
}
# url , templates 。
# url
# STATIC_URL MEDIA_URL
STATIC_URL = '/static/'
# static
# manage.py collectstatic static
# STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static').replace('\\', '/')
# app static static
#STATICFILES_DIRS: TEMPLATE_DIRS , app static ,
# 。 , Django,
# ,Django findstatic , :D:\TestDjango>python manage.py findstatic Chrome.jpg
# ( STATICFILES_FINDERS ),Django STATICFILES_DIRS , app static ,
# 。 STATICFILES_DIRS , app app static 。
# static , images , CSS css , js js 。
STATICFILES_DIRS = (
("downloads", os.path.join(PROJECT_ROOT, 'static/downloads').replace('\\', '/')),
("uploads", os.path.join(PROJECT_ROOT, 'static/uploads').replace('\\', '/')),
)
# app
STATICFILES_DIRS += MY_STATIC_DIRS
# STATICFILES_DIRS
# ,static , css js images ( ), :
# os.path.join(STATIC_ROOT,'css'),os.path.join(STATIC_ROOT,'js'),os.path.join(STATIC_ROOT,'images');
# css,js,images( , , )
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates').replace('\\', '/'),
)
#
TEMPLATE_DIRS += MY_TEMPLATE_DIRS
#
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:/tmp/memcached.sock',
'KEY_PREFIX': 'lcfcn',
'TIMEOUT': None
}
}
LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/auth/login/'
LOGOUT_URL = '/auth/logout/'
# , Model FileFIeld,ImageField 。
# MEDIA_ROOT=c:\temp\media, File=models.FileField(upload_to="abc/"), c:\temp\media\abc。MEDIA_ROOT 。
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'static/uploads')
# MEDIA_URL 。
MEDIA_URL = '/uploads/'
#
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.sitemaps',
)
# ,
# app
INSTALLED_APPS += MY_APPS
# django
# django Request django , URL 。
# django Request ,
# Response, ,
# Request , Django 。
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'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',
'django.middleware.cache.FetchFromCacheMiddleware',
)
ROOT_URLCONF = 'lcforum.urls'
WSGI_APPLICATION = 'lcforum.wsgi.application'
#
DATABASES = {
'default': MYDB.get('sqlite'),
}
#
LANGUAGE_CODE = 'zh-cn'
#
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = True
# template
# , settings.py, TEMPLATE_CONTEXT_PROCESSORS 'django.core.context_processors.static'
# TEMPLATE_CONTEXT_PROCESSORS = (
# 'django.core.context_processors.debug',
# 'django.core.context_processors.i18n',
# 'django.core.context_processors.media',
# 'django.core.context_processors.static',
# 'django.contrib.auth.context_processors.auth',
# 'django.contrib.messages.context_processors.messages',
#
# 'django.core.context_processors.tz',
# 'django.contrib.messages.context_processors.messages',
# # 'blog.context_processors.custom_proc',
# )
#from django.conf import settings
#gettext = lambda s: s
#getattr()
# djangodemo, app demo1 demo2
# django static app static
# :
# djangodemo/djangodemo/static
# djangodemo/demo1/static app
# djangodemo/demo2/static app
# :
# STATIC_ROOT = '/www/djangodemo/djangodemo/static '
# STATIC_URL = '/static/'
# STATICFILES_DIRS = (
# 'djangodemo/static',
# 'demo1/static/',
# 'demo2/static/',
# )
#
# manage.py collectstatic
# STATIC_ROOT
# admin, ,
# 。 manage.py collectstatic
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django의 질문 및 답변 웹사이트환영 친구, 이것은 우리의 새로운 블로그입니다. 이 블로그에서는 , 과 같은 Question-n-Answer 웹사이트를 만들고 있습니다. 이 웹사이트는 회원가입 및 로그인이 가능합니다. 로그인 후 사용자는 사용자의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.