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 逻辑
访问: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