Django_环境搭建与命令行工具django-admin.py & manage.py


常见的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.
View Code

 创建一个新的项目工程

(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.
View Code

访问http://localhost:8000/admin/ 

这就是框架的强大之处。

假如我想修改用户lichengguang的密码 我需要输入以下命令

manage.py changepassword lichengguang

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM