django版本:django1.11.16   windows环境  python 3.6.3 

 

!!!可先安装虚拟环境在进行环境搭建

 

 1.安装django:pip install django==1.11.*

 2.新建项目: django-admin startproject 项目名

 3.新建应用:  python manage.py startapp 应用名

 4.项目的url.py文件里导入include将符合主路由规则的路由引入到子路由中进行再分配

 

from django.conf.urls import url,include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^myadmin/', include('myadmin.urls')),
]
5.在应用的子路由中定义路由,使用HttpResponse返回一个字符串进行测试
return HttpResponse('ok')

6.init文件导入pymysql   安装pymysql: pip install pymysql
import pymysql
pymysql.install_as_MySQLdb()

7.setting设置
  
DEBUG = True    开启debug模式;实际工作中关闭debug模式

ALLOWED_HOSTS = ['*']   设置允许访问的域名


# Application definition 将应用名添加到列表中

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myhome',
'myadmin',
]
#中间件
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'web.urls'
#模板设置
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',
],
},
},
]

WSGI_APPLICATION = 'web.wsgi.application'

#数据库设置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'data',#选择数据库的名,请确认你的mysql中有这个库
'USER': 'root',
'PASSWORD': '123456',
'HOST': 'localhost',
'PORT': '3306',
}
}

# Password validation
# https://docs.djangoproject.com/en/1.11/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.11/topics/i18n/

LANGUAGE_CODE = 'zh-hans' #设置django的admin后台---中文汉字

TIME_ZONE = 'Asia/Shanghai'  #设置时区

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
#静态资源目录设置
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]