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     

 

좋은 웹페이지 즐겨찾기