django hellword

django hell world

建立項目 devops

# django-admin startproject deveops

建立app應用

# python3 manage.py startapp dashboard
# tree
.
├── dashboard
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── deveops
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   └── settings.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

設置項目路由

cd deveops/deveops
編輯urls.py
from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^dashboard/', include("dashboard.urls")),
]

增長app應用環境

在settings.py INSTALLED_APPS 模塊中增長dashboard應用
,增長後爲:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dashboard.apps.DashboardConfig'
]

配置app dashboard 視圖

在應用dashboard下修改views:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
    return HttpResponse("hello world !!!")

配置app dashboard 路由urls

from django.conf.urls import url
from .views import index
urlpatterns = [
    url(r'^hello/', index,name='index'),
]

啓動項目

python3 manage.py runserver 0.0.0.0:8888

訪問hello world

http://192.168.56.188:8888/dashboard/hello/

django hellword

django 邏輯

訪問:http://192.168.56.188:8888/dashboard/hello/
django 會加載settings 環境
當加載了項目的路由urls後 路徑http://192.168.56.188:8888/dashboard  會被路由到app應用dashboard的urls.py 中,
由於全路徑爲:http://192.168.56.188:8888/dashboard/hello/
urls.py規則中有:
urlpatterns = [
    url(r'^hello/', index,name='index'),
]
規則會把/hello/轉到視圖爲index 方法
因而乎就返回了views  視圖index方法的返回值
hello world
相關文章
相關標籤/搜索