Django快速入门(入门)

来源:互联网 发布:mac启动windows后黑屏 编辑:程序博客网 时间:2024/03/29 17:13

学习笔记,快速搭建一个可以跑的Django环境

0. 启动虚拟环境

xxx@xxx:~/py_script/pro-env$ lsbin  include  lib  localxxx@xxx:~/py_script/pro-env$ source bin/activate

1. 安装Django

(pro-env) xxx@xxx:~/py_script/pro-env$ pip install djangoCollecting django  Downloading Django-1.11.7-py2.py3-none-any.whl (6.9MB)    100% |████████████████████████████████| 7.0MB 107kB/s Collecting pytz (from django)  Downloading pytz-2017.3-py2.py3-none-any.whl (511kB)    100% |████████████████████████████████| 512kB 210kB/s Installing collected packages: pytz, djangoSuccessfully installed django-1.11.7 pytz-2017.3
  • 测试版本
(pro-env) xxx@xxx:~/py_script/pro-env$ pythonPython 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import django>>> django.get_version()'1.11.7'

3. 创建项目

# 创建项目mysite(pro-env) xxx@xxx:~/py_script/pro-env$ django-admin startproject mysite# 创建的文件list(pro-env) xxx@xxx:~/py_script/pro-env$ tree mysite/mysite/                 # 项目容器├── manage.py           # manager脚本└── mysite              # 此目录是项目的Pyhton包,导入这里面的内容时要使用目录的名称    ├── __init__.py    ├── settings.py    # 配置文件    ├── urls.py        # url声明    └── wsgi.py        # web服务器接口
  • 配置文件:
INSTALLED_APPS = [    'django.contrib.admin',         # 管理后台    'django.contrib.auth',          # 身份验证系统    'django.contrib.contenttypes',  # 内容类型框架    'django.contrib.sessions',      # 会话框架    'django.contrib.messages',      # 消息框架    'django.contrib.staticfiles',   # 管理静态文件的框架]

Django 项目默认包含这些应用,这是为常见场景所做的约定。其中某些应用要使用数据库表,因此使用之前要在数据库中创建所需的表。

(pro-env) xxx@xxx:~/py_script/pro-env/mysite$ python manage.py migrateOperations to perform:  Apply all migrations: admin, auth, contenttypes, sessionsRunning migrations:  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 auth.0008_alter_user_username_max_length... OK  Applying sessions.0001_initial... OK

4. 开发服务器

(pro-env) xxx@xxx:~/py_script/pro-env/mysite$ python manage.py runserverPerforming system checks...System check identified no issues (0 silenced).November 11, 2017 - 11:55:32Django version 1.11.7, using settings 'mysite.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.

可以访问到web页面

原创粉丝点击