常見的Python Web框架:
Full-Stack Frameworks(全棧框架,重量級):
django
web2py
TurboGears
Pylons
......
Non Full-Stack Frameworks(非全棧框架,輕量級):
Tornado
Flask
Bottle
web.py
Pyramid
......
Django開發環境的搭建:
開發環境說明:
Windows 10 64位
Python 3.5.2
Django 1.9.5
PyMySQL 0.7.2
Mysql 5.6
Sublime Text 3
Pycharm 5.05
1,首先安裝Python3.5.2
Python官網:https://www.python.org/
點擊“windows x86-64 executable installer”進行下載;
安裝時勾選Add Python3.5 to PATH 選項或者安裝后再將python根目錄添加至環境變量
各個版本區別:
web-based installer 是需要通過聯網完成安裝的
executable installer 是可執行文件(*.exe)方式安裝
embeddable zip file 嵌入式版本,可以集成到其它應用中
2.升級pip
cmd終端中:
python -m pip install --upgrade pip
3.創建虛擬環境
安裝virtualenv命令:pip install virtualenv 指定版本安裝: pip install virtualenv==15.0.1
創建虛擬環境命令:virtualenv [–no-site-packagesd] jango_basic_venv
創建好之后執行 Scripts\activate來進入虛擬環境安裝django
安裝Django :pip install django==1.9.5
安裝PyMySQL:pip install pymysql==0.7.2
C:\Users\lichengguang>pip install virtualenv==15.0.1
Collecting virtualenv==15.0.1
Downloading virtualenv-15.0.1-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 548kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.1
C:\Users\lichengguang>cd C:/
C:\>mkdir workspace
C:\>cd workspace
C:\workspace>mkdir venv
C:\workspace>cd venv
C:\workspace\venv>virtualenv django_basic_venv
Using base prefix 'c:\\python3'
New python executable in C:\workspace\venv\django_basic_venv\Scripts\python.exe
Installing setuptools, pip, wheel...done.
C:\workspace\venv>django_basic_venv\Scripts\activate
(django_basic_venv) C:\workspace\venv>pip install django==1.9.5
Collecting django==1.9.5
Downloading Django-1.9.5-py2.py3-none-any.whl (6.6MB)
100% |████████████████████████████████| 6.6MB 73kB/s
Installing collected packages: django
Successfully installed django-1.9.5
(django_basic_venv) C:\workspace\venv>pip install pymysql==0.7.2
Collecting pymysql==0.7.2
Downloading PyMySQL-0.7.2-py2.py3-none-any.whl (76kB)
100% |████████████████████████████████| 81kB 41kB/s
Installing collected packages: pymysql
Successfully installed pymysql-0.7.2
查看當前環境:
(django_basic_venv) C:\workspace\venv>pip freeze appdirs==1.4.3 Django==1.9.5 packaging==16.8 PyMySQL==0.7.2 pyparsing==2.2.0 six==1.10.0 (django_basic_venv) C:\workspace\venv>deactivate # 退出虛擬環境 C:\workspace\venv>pip freeze virtualenv==15.0.1
命令行工具 django-admin.py & manage.py
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語句(*)
dumpdata:導出數據
loaddata:導入數據
diffsettings:查看你的配置和django默認配置的不同之處
......
manage.py 特有的一些子命令:
createsuperuser :創建超級管理員(*)
changepassword:修改密碼(*)
clearsessions:清除session
......
我們還可以通過一下命令查看相應命令的用法
django-admin.py help
django-admin.py help startproject
1 c:\workspace\venv\django_basic_venv>Scripts\activate 2 3 (django_basic_venv) c:\workspace\venv\django_basic_venv>django-admin help 4 5 Type 'django-admin help <subcommand>' for help on a specific subcommand. 6 7 Available subcommands: 8 9 [django] 10 check 11 compilemessages 12 createcachetable 13 dbshell 14 diffsettings 15 dumpdata 16 flush 17 inspectdb 18 loaddata 19 makemessages 20 makemigrations 21 migrate 22 runserver 23 sendtestemail 24 shell 25 showmigrations 26 sqlflush 27 sqlmigrate 28 sqlsequencereset 29 squashmigrations 30 startapp 31 startproject 32 test 33 testserver 34 Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.). 35 36 (django_basic_venv) c:\workspace\venv\django_basic_venv>django-admin help startproject 37 usage: django-admin startproject [-h] [--version] [-v {0,1,2,3}] 38 [--settings SETTINGS] 39 [--pythonpath PYTHONPATH] [--traceback] 40 [--no-color] [--template TEMPLATE] 41 [--extension EXTENSIONS] [--name FILES] 42 name [directory] 43 44 Creates a Django project directory structure for the given project name in the 45 current directory or optionally in the given directory. 46 47 positional arguments: 48 name Name of the application or project. 49 directory Optional destination directory 50 51 optional arguments: 52 -h, --help show this help message and exit 53 --version show program's version number and exit 54 -v {0,1,2,3}, --verbosity {0,1,2,3} 55 Verbosity level; 0=minimal output, 1=normal output, 56 2=verbose output, 3=very verbose output 57 --settings SETTINGS The Python path to a settings module, e.g. 58 "myproject.settings.main". If this isn't provided, the 59 DJANGO_SETTINGS_MODULE environment variable will be 60 used. 61 --pythonpath PYTHONPATH 62 A directory to add to the Python path, e.g. 63 "/home/djangoprojects/myproject". 64 --traceback Raise on CommandError exceptions 65 --no-color Don't colorize the command output. 66 --template TEMPLATE The path or URL to load the template from. 67 --extension EXTENSIONS, -e EXTENSIONS 68 The file extension(s) to render (default: "py"). 69 Separate multiple extensions with commas, or use -e 70 multiple times. 71 --name FILES, -n FILES 72 The file name(s) to render. Separate multiple 73 extensions with commas, or use -n multiple times.
創建一個新的項目工程
(django_basic_venv) c:\workspace\pycharm>django-admin.py startproject hello_django #創建工程 (django_basic_venv) c:\workspace\pycharm>cd hello_django # 進入工程目錄去創建app (django_basic_venv) c:\workspace\pycharm\hello_django>django-admin.py startapp hello # 創建app (django_basic_venv) c:\workspace\pycharm\hello_django>manage.py runserver # 啟動服務 使用默認ip和默認端口 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. April 19, 2017 - 16:16:55 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/ (或者http://localhost:8000/)即可得到 It worked!
也可以設置特定的ip和端口 : manage.py runserver 0.0.0.0 9090
下面創建一個admin用戶。
Ctrl+C關掉 服務器 創建數據庫,創建超級用戶,設置用戶名,郵箱和密碼,啟動服務。
manage.py makemigrations
manage.py migrate
manage.py createsuperuser
manage.py runserver
1 (django_basic_venv) c:\workspace\pycharm\hello_django>manage.py makemigrations 2 No changes detected 3 4 (django_basic_venv) c:\workspace\pycharm\hello_django>manage.py migrate 5 Operations to perform: 6 Apply all migrations: auth, admin, sessions, contenttypes 7 Running migrations: 8 Rendering model states... DONE 9 Applying contenttypes.0001_initial... OK 10 Applying auth.0001_initial... OK 11 Applying admin.0001_initial... OK 12 Applying admin.0002_logentry_remove_auto_add... OK 13 Applying contenttypes.0002_remove_content_type_name... OK 14 Applying auth.0002_alter_permission_name_max_length... OK 15 Applying auth.0003_alter_user_email_max_length... OK 16 Applying auth.0004_alter_user_username_opts... OK 17 Applying auth.0005_alter_user_last_login_null... OK 18 Applying auth.0006_require_contenttypes_0002... OK 19 Applying auth.0007_alter_validators_add_error_messages... OK 20 Applying sessions.0001_initial... OK 21 22 (django_basic_venv) c:\workspace\pycharm\hello_django>manage.py createsuperuser 23 Username (leave blank to use 'lichengguang'): lichengguang 24 Email address: lcgsmile@qq.com 25 Password: 26 Password (again): 27 Superuser created successfully. 28 29 (django_basic_venv) c:\workspace\pycharm\hello_django>manage.py runserver 30 Performing system checks... 31 32 System check identified no issues (0 silenced). 33 April 19, 2017 - 16:30:53 34 Django version 1.9.5, using settings 'hello_django.settings' 35 Starting development server at http://127.0.0.1:8000/ 36 Quit the server with CTRL-BREAK.
訪問http://localhost:8000/admin/


這就是框架的強大之處。
假如我想修改用戶lichengguang的密碼 我需要輸入以下命令
manage.py changepassword lichengguang
