一.开发环境的搭建:
1.安装python
2.更新pip版本:
python -m pip install --upgrade pip
3.使用pip安装virtualenv:
pip install virtualenv==15.0.1
4.创建虚拟环境:
创建 :virtualenv django_basic_venv
5.使用虚拟环境:
django_basic_venv\Scripts\activate
命令前面出现(django_basic_venv)表示使用到该虚拟环境。
退出虚拟环境:deactivate
6.在虚拟环境中安装django:
pip install django==1.9.5
6.在虚拟环境中安装pymysql:
pip install pymysql==0.7.2
至此环境搭建完毕 ,用pip freeze命令查看当前环境:
(django_basic_venv) E:\Workspace\django_basic_venv>pip freeze
Django==1.9.5
PyMySQL==0.7.2
二.命令行工具
django-admin.py是Django的一个用于管理任务的命令行工具,manage.py是对django-admin.py的简单包装,每个Django Project里面都会包含一个manage.py
语法:
django-admin.py <subcommand> [options]
manage.py <subcommand> [options]
subcommand 是子命令;options是可选的
常用子命令:
startproject:创建一个项目
startapp:创建一个app
runserver:运行开发服务器
shell:进入django shell
dbshell:进入django dbshell
check:检查django项目完整性
flush:清空数据库
compilemessages:编译语言文件
makemessages:创建语言文件
makemigrations:生成数据库同步脚本
migrate:同步数据库
showmigrations:查看生成的数据库同步脚本
sqlflush:查看生成清空数据库的脚本
sqlmigrate:查看数据库同步的sql语句
dumpdate:导出数据
loaddate:导入数据
diffsettings:查看你的配置和Django配置的不同之处
...
manage.py特有的一些子命令:
createsuperuser:创建超级管理员
changepassword:修改密码
clearsessions:清除session
...
简单演示第一个项目:
C:\Users\lcg>e:
E:\>Workspace\django_basic_venv\Scripts\activate // 进入虚拟环境
(django_basic_venv) E:\>cd Workspace\pycharm // 切换到项目目录
(django_basic_venv) E:\Workspace\pycharm>django-admin.py startproject hello_django // 创建项目
(django_basic_venv) E:\Workspace\pycharm>cd hello_django // 进入项目目录
(django_basic_venv) E:\Workspace\pycharm\hello_django>django-admin.py startapp hello // 创建app
(django_basic_venv) E:\Workspace\pycharm\hello_django>manage.py runserver //运行开发服务器
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run ‘python manage.py migrate‘ to apply them.
June 26, 2017 - 17:38:32
Django version 1.9.5, using settings ‘hello_django.settings‘
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
浏览器中输入网址: http://127.0.0.1:8000/ 得到下面效果。
下面按住Ctrl C 终止当前运行的服务器 同步数据库再创建一个admin账号然后启动开发服务器再浏览:http://127.0.0.1:8000/admin
(django_basic_venv) E:\Workspace\pycharm\hello_django>manage.py makemigrations // 生成数据库同步脚本
No changes detected
(django_basic_venv) E:\Workspace\pycharm\hello_django>manage.py migrate // 同步数据库
Operations to perform:
Apply all migrations: auth, sessions, contenttypes, admin
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying sessions.0001_initial... OK
(django_basic_venv) E:\Workspace\pycharm\hello_django>manage.py createsuperuser 创建一个admin账号
Username (leave blank to use ‘lcg‘): lcg
Email address: [email protected]
Password: // 此处密码不可以太简单 !
Password (again):
Superuser created successfully.
(django_basic_venv) E:\Workspace\pycharm\hello_django>manage.py runserver // 启动开发服务器
Performing system checks...
System check identified no issues (0 silenced).
June 26, 2017 - 17:58:05
Django version 1.9.5, using settings ‘hello_django.settings‘
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
浏览:http://127.0.0.1:8000/admin 输入上面创建好的账号密码
Log in :
修改admin账号的密码 :
(django_basic_venv) E:\Workspace\pycharm\hello_django>manage.py changepassword lcg
Changing password for user ‘lcg‘
Password:
Password (again):
Password changed successfully for user ‘lcg‘