django基礎入門

django 環境安裝

使用Centos7.4 python2.7.5(默認)

源碼地址python

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

./web/web/settings.py
修改監聽地址git

ALLOWED_HOSTS = ['10.10.10.35','127.0.0.1']   # 表明監聽2個IP
ALLOWED_HOSTS = ['*']                         # 表明監聽本機全部IP

啓動web

python manage.py runserver 0:8000             # 0:8000  表明使用配置的ip地址

修改根路由 web/web/urls.py sql

from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
      #url(r'^admin/', admin.site.urls),
    url(r'^', include('myweb.urls')),
]

修改子路由 web/myweb/urls.pydjango

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),
]

建立視圖函數 web/myweb/views.py centos

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def hello(request):
    return HttpResponse('hello word')

頁面訪問 10.10.10.35:8000/hello app

相關文章
相關標籤/搜索