django 环境安装
使用Centos7.4 python2.7.5(默认)
源码地址
git clone https://gitee.com/Outsrkem/web.git
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel
sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum -y install epel-release
yum -y install python-pip
cat << EOF > ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
EOF
pip install --upgrade pip
pip install django
创建模板
django-admin startproject web
创建项目
python manage.py startapp myweb
应用项目(如果是迁移过来的)
python manage.py migrate
启动项目
python manage.py runserver
web/
├── db.sqlite3
├── manage.py
├── myweb
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── web
├── __init__.py
├── settings.py # 总配置文件
├── urls.py
├── wsgi.py
<font face="Courier New"color='#0A0A0A'> ./web/web/settings.py
修改监听地址
ALLOWED_HOSTS = ['10.10.10.35','127.0.0.1'] # 代表监听2个IP
ALLOWED_HOSTS = ['*'] # 代表监听本机所有IP
启动
python manage.py runserver 0:8000 # 0:8000 代表使用配置的ip地址
修改根路由 <font face="Courier New"color='#0A0A0A'> web/web/urls.py
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^', include('myweb.urls')),
]
修改子路由 <font face="Courier New"color='#0A0A0A'>web/myweb/urls.py
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^hello/', views.hello),
]
创建视图函数<font face="Courier New"color='#0A0A0A'> web/myweb/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def hello(request):
return HttpResponse('hello word')
页面访问 <font face="Courier New"color='#0A0A0A'> 10.10.10.35:8000/hello