django简单demo
环境:CentOS 7.2
python2.7.12
django1.8
一.创建vitualenv并安装django
virtualenv dj1.8
source dj1.8/bin/activate
pip install django==1.8
二.创建project
mkdir project
django-admin startproject blog project/
三.配置
project/blog/settings.py
提示:服务正常run之后,配置文件修改无需重启服务即可生效
i.授权监听地址
ALLOWED_HOSTS = ['127.0.0.1','localhost','192.168.43.144','192.168.8.254']
否则会有的通有ip能访问有的不能
ii.数据库配置
1.建库与授权
默认的sqlite3可以直接启动,这里使用mysql,mysql安装配置请参看MariaDB-10.1.x二进制包安装
create database django character set utf8;
grant all on django.* to 'djadmin'@'%' identified by 'djpassword' with grant option;
flush privileges;
2.安装mysql驱动
pip install MySQL-python
3.db连接配置
DATABASES = {
}
提示:认证信息可以直接明文写入,也可以通过options来调用外部配置。本文从变量中获取
4.初始化db
python manage.py migrate
5.创建superuser
python manage.py createsuperuser
其它配置
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
四.启动
python manage.py runserver 0.0.0.0:8000
192.168.43.144:8000/admin/
后台美化示例
https://github.com/django-admin-bootstrap/django-admin-bootstrap
五.创建app
django-admin startapp project/blog/app #或python project/blog/manage.py startapp app
i.导入app
project/blog/settings.py
INSTALLED_APPS = (
)
ii.后台自定义表单
1.project/blog/app/models.py
from django.db import models
# Create your models here.