Django 入門

安裝好後進入文件夾會看到裏面Django爲咱們建立好的文件裏python

 

manage.py 爲咱們啓動服務的文件  python manage.py runserver 8080(啓動服務的時候默認是8000,後面加8080 能夠給他設置啓動服務的端口號)sql

db.sqlite3 這個文件是爲咱們提取建立好的鏈接數據庫的文件夾數據庫

 

進入mysite內django

 

 __init__.py 爲初始化文件session

settings.py 是配置文件app

urls.py 是設置路由文件ide

wsgi.pt 測試

 

咱們進入settings.py看一下相關配置(我在下圖已經把相關配置對應的做用標準上備註)ui

"""
Django settings for mysite project.

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

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

 #imprt os 模塊:
 #在自動化測試中,常常須要查找操做文件,好比說查找配置文件(從而讀取配置文件的信息),查找測試報告(從而發送測試報告郵件),常常要對大量文件和大量路徑進行操做,這就依賴於os模塊this

# 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/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
#密鑰
SECRET_KEY = '3pf%rp#u@h9xu&@utms#kl#=)t(rua=g6h)8lyp$9(7quv9h7!'

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

ALLOWED_HOSTS = []


# Application definition
#已安裝的django應用
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
#中間件模式
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    #備註是的代碼爲禁止POST請求由於我要用到因此我註釋掉了,你們按需求選性便可。
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    #本地化支持,這是我在原有基礎上加,它的做用是判斷你本地ip從而幫你翻譯Django界面
    'django.middleware.locale.LocaleMiddleware',
]
#定義路由文件
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/2.0/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/2.0/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/2.0/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/2.0/howto/static-files/

# 定義靜態文件目錄
STATIC_URL = '/static/'
相關文章
相關標籤/搜索