Best configuration of django settings, djangosettings
# Encoding = utf-8import osimport socketSITE_ID = 1 # project root directory # simplify the subsequent operations PROJECT_ROOT = OS. path. dirname (OS. path. dirname (_ file _) # load the application # Add the application to INSTALLED_APPS from apps. kuser. mysetting import myapp as kuser_appfrom apps. blog. mysetting import myapp as blog_appMY_APPS = blog_app + kuser_app # Load Static files from apps. blog. mysetting import my_staticfiles as blog_staticfilesfrom apps. kuser. mysetting import my_staticfil Es as kuser_staticfilesMY_STATIC_DIRS = blog_staticfiles + kuser_staticfiles # load the template file from apps. blog. mysetting import my_templates as blog_templatesfrom apps. kuser. mysetting import my_templates as kuser_templatesMY_TEMPLATE_DIRS = blog_templates + kuser_templates # key configuration # Applicable to development and deployment environments # You can obtain the key try from the system environment, configuration files, and hard-coded configuration: SECRET_KEY = OS. environ ['secret _ key'] character T: try: with open (OS. path. join (PROJECT_ROOT, 'Db/secret_key '). replace ('\', '/') as f: SECRET_KEY = f. read (). strip () Counter T: SECRET_KEY = '* lk ^ 6 @ 0l0 (iulgar $ j) faff & ^ (^ u + qk3j73d18 @ & + ur ^ xuTxY '# obtain the host name 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') # debugging and template debugging configuration # if the host name is the same as the development environment, the deployment environment is different # ALLOWED_HOSTS can be empty only in the debugging environment. gethostname (). lower () = hostname (). lower (): DEBUG = TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] else: ALLOWED_HOSTS = ['Baidu. com ', '0. 0.0.0 ',] DEBUG = TEMPLATE_DEBUG = False # Database Configuration MYDB = {'mysql': {'Engine': 'django. db. backends. mysql ', 'name': 'books', # Your Database NAME 'user': 'root', # Your database username 'Password ':'', # Your Database Password 'ho St': '', # Your Database Host. If you leave it blank, the default value is localhost 'Port': '123', # Your Database PORT}, and 'sqlite ': {'Engine ': 'django. db. backends. sqlite3 ', 'name': OS. path. join (PROJECT_ROOT, 'db/db. sqlite3 '). replace ('\', '/'),} # A suffix for the static File url, which is used in templates. # Ing to the url of the static file # The meaning of STATIC_URL is similar to that of MEDIA_URL: STATIC_URL = '/static/' # total static directory # You can use the command manage. py collectstatic automatically collects static files # STATIC_ROOT = OS. path. join (PROJECT_ROOT, 'static '). replace ('\', '/') # Put the static directory of each app and the public static directory # STATICFILES_DIRS: similar to TEMPLATE_DIRS, it is the static file settings that need to be managed in addition to the static directory of each app, # For example, the Public file of the Project is similar. Assign a value to the static file variable to tell Django where the static file is located # In addition, Django provides a findstatic command to find the directory where the specified static file is located, for example: D: \ TestDjango> python manage. py findstatic Chrome.jpg # By default (if STATICFILES_FINDERS is not modified), Django first searches for static files in the STATICFILES_DIRS configuration folder, and then searches for them in the static subdirectory of each app, # Return the first file found. Therefore, we can place the Global static files in the STATICFILES_DIRS configuration directory and put the static files unique to the app in the static subdirectory of the app. # When storing files, the files are stored in the subdirectory of the static directory by category. All the files are placed in the images folder, all CSS files are placed in the css folder, and all js files are placed in the js folder. STATICFILES_DIRS = ("downloads", OS. path. join (PROJECT_ROOT, 'static/downloads '). replace ('\', '/'), ("uploads", OS. path. join (PROJECT_ROOT, 'static/uploads '). replace ('\\','/')),) # Add the static files in the app to the STATICFILES_DIRS + = MY_STATIC_DIRS in the static file configuration list # The final key part is the configuration below STATICFILES_DIRS # briefly speaking, the static folder is in the project, there are three css js images folders (depending on the project structure). Their paths are: # OS. path. join (STATIC_ROOT, 'css '), OS. path. join (STATIC_ROOT, 'js'), OS. path. Join (STATIC_ROOT, 'images'); # We will give them three aliases: css, js, and images (you can give them at will, but we have specified the alias for the sake of ease of remembering) TEMPLATE_DIRS = (OS. path. join (PROJECT_ROOT, 'templates '). replace ('\', '/'),) # configure the application's template file path TEMPLATE_DIRS + = MY_TEMPLATE_DIRS # configure cache CACHES = {'default': {'backend ': 'django. core. cache. backends. memcached. memcachedCache ', 'location': 'unix:/tmp/memcached. sock ', 'key _ prefix': 'lcfcn', 'timeout': None} LOGIN_REDIRECT_UR L = '/'login _ URL ='/auth/LOGIN/'logout _ URL = '/auth/LOGOUT/' # indicates the File Uploaded by the user, for example, FileFIeld and ImageField files in the Model. If you define # MEDIA_ROOT = c: \ temp \ media, File = models. fileField (upload_to = "abc/"), the uploaded file is saved to c: \ temp \ media \ abc. MEDIA_ROOT must be the absolute path of the local path. MEDIA_ROOT = OS. path. join (PROJECT_ROOT, 'static/uploads') # MEDIA_URL indicates the address prefix when accessing from a browser. 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 ',) # To avoid mixing with system applications, put your own applications here # add your own apps to the Application List to INSTALLED_APPS + = MY_APPS # django middleware # django's process of processing a Request is first through django middleware, then, use the default URL method. # What we need to do is intercept all requests in the django middleware. # Return Response directly after processing in our own way, so we can simplify the original design idea, # discard all the requests that cannot be processed by the middleware and send them to Django for processing. 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 '# Database Configuration DATABASES = {'default': MYDB. get ('sqlite '),} # LANGUAGE_CODE = 'zh-cn' # TIME_ZONE = 'Asia/Shanghai' USE _ TZ = True # USE static files in the template # some additional configurations are required for this method, open settings. py, make sure TEMPLATE_CONTEXT_PROCESSORS contains 'django. cor E. 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. conte Xt_processors.messages ', # 'blog. context_processors.custom_proc ', UDF #) # from django. conf import settings # gettext = lambda s: s # getattr () # assume there is a project djangodemo, there are two apps demo1 and demo2 # The method for django to handle static operations is to merge the static operations of each app into one place # For example: # djangodemo/static place public static files # djangodemo/demo1/static place the app's own static files # djangodemo/demo2/static place the app's own static files # You can set this setting: # STATIC_ROOT = '/www/djangodemo/static '# STATIC_URL = '/static/' # STATICFILES_DIRS = (# 'djangodemo/static ', #'demo1/static/', #'demo2/static /',#) # Use the command # manage. py collectstatic # All static files will be automatically copied to STATIC_ROOT # This step is necessary if admin is enabled, otherwise, the style file cannot be found when deployed to the production environment # Do not put the static files of your project into this directory. This directory is used only when manage. py collectstatic is run.