django框架结构和基本命令

2018-11-27  本文已影响0人  joyitsai

1. MTV模型

web框架,指为解决一个开放性的问题而设计的具有一定约束性的支撑架构。

对于所有的web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端。

MTV模型拆分开来讲,就是下面三个部分:

2. Django原理

图1.1: django框架的工作流程原理

3. Django项目开发流程及基本命令

1. 安装django

python2 –m pip install django==1.8.2
pip install django==1.8.2

2. 创建一个django项目

(django_py2) kakyoku@ubuntu:~/Desktop/Projects$ django-admin startproject myWeb

3. 创建django应用

(django_py2) kakyoku@ubuntu:~/Desktop/Projects/myWeb$ python manage.py startapp myBlog

通过tree命令查看myWeb项目中的目录结构:

(django_py2) kakyoku@ubuntu:~/Desktop/Projects/myweb$ tree
.
├── db.sqlite3
├── manage.py
├── myBlog
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── myweb
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py
3 directories, 16 files

其中:

4. 启动django项目

在项目目录下,通过manage.py脚本工具可以启动django项目,同时可以指定端口:

(django_py2) kakyoku@ubuntu:~/Desktop/Projects/myweb$ python manage.py runserver 8080
Performing system checks...
System check identified no issues (0 silenced).
November 26, 2018 - 01:57:54
Django version 1.8.2, using settings 'myweb.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.

5. 同步数据库

python manage.py makemigrations
python manage.py migrate

通过python manage.py migrate同步数据库的话,需要手动创建超级管理员,输入用户名、邮箱、密码:

python manage.py createsuperuser
Username (leave blank to use 'kakyoku'): admin
Email address: admin@qq.com
Password: 
Password (again): 
Superuser created successfully.

通过python manage.py syncdb同步数据库时,会自动创建超级管理员,当然需要你填写用户名、邮箱和密码:

python manage.py validate #检验数据模型Model语法合法性
python manage.py makemigrations #数据库语句生成
python manage.py syncdb #将数据模型同步到数据库,同时自动创建超级管理员账号
(django_py2) kakyoku@ubuntu:~/Desktop/Projects/myweb$ python manage.py syncdb
/home/kakyoku/.virtualenvs/django_py2/local/lib/python2.7/site-packages/django/core/management/commands/
syncdb.py:24:RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... 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 sessions.0001_initial... OK

You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): y
Please enter either "yes" or "no": yes
Username (leave blank to use 'kakyoku'): admin
Email address: admin@qq.com
Password: 
Password (again): 
Superuser created successfully.
上一篇下一篇

猜你喜欢

热点阅读