windows 64位
1、python 3.6.6 64位
2、pip install virtualenv
3、切换目录,创建虚拟环境 virtualenv py_django
4、pip install Django==2.0.7
5、https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient
mysqlclient-1.3.13-cp36-cp36m-win_amd64.whl
6、pip install pymysql
7、启动虚拟环境
Scripts\activate
8、退出虚拟环境
deactivate
===========================================================
1、django-admin startproject project_name
2、cd project_name
3、python manage.py startapp project_app
4、setting.py
4.1、
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
4.2、
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '数据库名称',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': {
'autocommit': True,
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
create database 数据库名称 character set utf8;
4.3、
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'project_app'
]
5、urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('project_app/', include('project_app.urls')),
]
6、project_app文件夹下,新建templates、static
7、在templates、static 文件夹下面分别新建一个文件夹,名称位:project_app
8、project_app文件夹下,新建urls.py
9、新建的urls.py
from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home, name='home')
]
10、views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse('hello world')
11、数据库初始化 python manage.py migrate
12、创建管理员 python manage.py createsuperuser
13、启动 python manage.py runserver 9000
端口号可自定义
14、http://localhost:9000
15、http://localhost:9000/admin