django settings相關配置

 settings

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.9.8.

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

# 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 = '%$-pyxdf_@3&)l%%@#(&d(c@f3awodh9*h42zr^khx0zay6sfq'

# SECURITY WARNING: don't run with debug turned on in production!
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',
]

MIDDLEWARE_CLASSES = [
    '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 = 'mysite.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 = 'mysite.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/'
settings

一、註冊app:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01',
]

二、配置數據庫

#鏈接mysql數據庫

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER':'root',
'PASSWORD':'xxxxxx',
'HOST':'',  #默認localhost
'PORT':'',  #默認8000
    }
}

ps:
# 因爲Django內部鏈接MySQL時使用的是MySQLdb模塊,而python3中還無此模塊,因此須要使用pymysql來代替
  
# 以下設置放置的與project同名的配置的 __init__.py文件中
  
import pymysql
pymysql.install_as_MySQLdb() 

三、模版

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ os.path.join(BASE_DIR,'templates')],
'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',
],
},
},
]

四、靜態文件

STATIC_URL = '/static/'    #前綴
#STATIC_URL定義了當Django運行時,django應用尋找靜態媒體的地址。例如STATIC_URL = '/static/' ,STATIC_URL設置成/static/,咱們就能夠經過http://127.0.0.1:8000/static/來訪問它了

STATICFILES_DIRS = (
        os.path.join(BASE_DIR,'static'),
    )
#STATICFILES_DIRS容許定義新的static目錄。像TEMPLATE_DIRS元組同樣.STATICFILES_DIRS須要static目錄的絕對路徑.

五、配置上傳文件夾(media)並設置爲靜態可訪問文件夾

#settings

#配置上傳文件夾
MEDIA_URL = '/media/'  # 設置媒體文件的相對路徑
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')  # 設置媒體文件的絕對路徑

STATICFILES_DIRS = (
   os.path.join(BASE_DIR, 'static'),
   os.path.join(BASE_DIR, 'media'),
)

#設置爲靜態url可訪問文件夾
##project.url
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
  
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # 配置media

 

六、語言與時區

LANGUAGE_CODE = 'zh-Hans'#語言 

TIME_ZONE = 'Asia/Shanghai' #時區

七、調試模式

DEBUG = True
#DEBUG=True 時,若是出現 bug 便於咱們看見問題所在,可是部署時最好不要讓用戶看見bug的詳情,可能一些不懷好心的人攻擊網站,形成沒必要要的麻煩。
相關文章
相關標籤/搜索