Django快速入门(入门)

本文介绍了如何在虚拟环境中安装Django并创建一个简单的项目。包括激活虚拟环境、安装Django、创建项目、配置数据库及运行开发服务器等步骤。

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

0. 启动虚拟环境

xxx@xxx:~/py_script/pro-env$ ls
bin  include  lib  local
xxx@xxx:~/py_script/pro-env$ source bin/activate

1. 安装Django

(pro-env) xxx@xxx:~/py_script/pro-env$ pip install django
Collecting 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, django
Successfully installed django-1.11.7 pytz-2017.3
  • 测试版本
(pro-env) xxx@xxx:~/py_script/pro-env$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "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 migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running 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 runserver
Performing system checks...

System check identified no issues (0 silenced).
November 11, 2017 - 11:55:32
Django 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页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值