mysite
├── manage.py 管理项目:包括数据库建立、服务器运行、测试……
└── mysite
├── __init__.py
├── settings.py 配置文件:应用、中间件、数据库、静态目录各种配置……
├── urls.py URL映射配置文件L决定一个url访问被哪个程序(函数)响应
└── wsgi.py Python应用程序或框架和Web服务器之间接口
运行python manage.py可以看到其后可以跟的一些命令:
[auth]
changepassword 更改超级用户的密码
createsuperuser 创建一个超级用户(用于管理django后台) [django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate 用于创建数据库的
sendtestemail
shell 给我们提供一个用于调试的shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver [sessions]
clearsessions [staticfiles]
collectstatic
findstatic
runserver
python manage.py后面带的参数
通过命令:python manage.py runserver 启动一个开发服务器。
通过命令:python manage.py runserver 0.0.0.0:8080 更改服务器启动的host和端口(变为http://0.0.0.0:8080)。
通过命令:python manage.py shell 可以进入django提供给我们的一个交互式命令行。在该交互式命令行中可以进行很多操作,如直接访问数据库。
"""
Django settings for mysite project. Generated by 'django-admin startproject' using Django 1.9.5. 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 = 'uva%@my)iwu(qhj98()oki*xe@k+s-difty&248rym9*b^f20-' # 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.py
整个网站的所有配置相关信息都在setting.py中。
ALLOWED_HOST : 允许访问的IP域
INSTALLED_APPS : 已经安装的引用
MIDDLEWARE_CLASSES : 已经安装的中间件
ROOT_URLCONF = 'mysite.urls' URL的根目录
TEMPLATES : 模板引擎
DATABASES : 数据库引擎
LANGUAGE_CODE : 网站的语言
TIME_ZONE : 时区
"""mysite URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urls.py
urlpatterns表中有关于映射的所有信息。
"""
WSGI config for mysite project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
""" import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") application = get_wsgi_application()
wsgi.py
django起的是一个轻量级的调试服务器,真正做到产品的时候是用的大型的服务器。
django和大型服务器之间的接口是通过wsgi来配置的。