step1:新建一個工作目錄WorkSpace/website
step2:在website目錄下創(chuàng)建Django項目salviaFlora
?????? django-adminstartproject salviaFlora
?????? 當(dāng)前目錄結(jié)構(gòu)
.
├──manage.py
└──salviaFlora
??? ├──asgi.py
??? ├──__init__.py
??? ├── settings.py #網(wǎng)站整體的配置文件
??? ├── urls.py #網(wǎng)站整體的路由映射表
└──wsgi.py
step3:創(chuàng)建一個App狂票,并將該App放到apps文件夾里面,notes:apps與salviaFlora處于同級目錄
?????? mkdirapps
?????? mkdirapps/salviaInfo
?????? pythonmanage.py startapp salviaInfo ./apps/salviaInfo
創(chuàng)建完成后網(wǎng)站的目錄結(jié)構(gòu)如下
? ? ? .
├──apps
│?? └──salviaInfo
│?????? ├──admin.py
│?????? ├──apps.py
│?????? ├──__init__.py
│?????? ├──migrations
│?????? │?? └──__init__.py
│?????? ├──models.py
│?????? ├──tests.py
│?????? └──views.py
├──manage.py
└──salviaFlora
??? ├──asgi.py
??? ├──__init__.py
??? ├──__pycache__
??? │?? ├──__init__.cpython-37.pyc
??? │?? └──settings.cpython-37.pyc
??? ├──settings.py
??? ├──urls.py
??? └──wsgi.py
?????? step4:配置salviaInfo/setting.py文件
import?os
import?sys
#?Build?paths?inside?the?project?like?this:?os.path.join(BASE_DIR,?...)
BASE_DIR?=?os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,os.path.join(BASE_DIR,'apps'))#將apps文件夾加入到pythonpath中,以被python識別該目錄下的app
ALLOWED_HOSTS?=?['*',]#允許通過外網(wǎng)ip訪問
#?Application?definition
INSTALLED_APPS?=?[
????'django.contrib.admin',
????'django.contrib.auth',
????'django.contrib.contenttypes',
????'django.contrib.sessions',
????'django.contrib.messages',
????'django.contrib.staticfiles',
????'salviaInfo',#注冊salviaInfo
]
step5:配置路由映射表salviaFlora/urls.py
?????? 輸入https://www.salviaflora.com/index/后django送到后臺的只有index/這個字符串,然后與path()第一個參數(shù)匹配,匹配成功后送到下一級路由映射表include()
from?django.contrib?import?admin
from?django.urls?import?path,include
urlpatterns?=?[
????path('admin/',?admin.site.urls),
????path('',include('salviaInfo.urls'))#拿到index/后探入,匹配成功,然后送到salviaInfo的路由映射表.(''與任何字符串都能匹配成功)
]
step6:進入apps/salviaInfo,配置views.py文件
from?django.shortcuts?import?render
from?django.http?import?HttpResponse
#?Create?your?views?here.
def?index(request):
????return?HttpResponse('hello')
step7:在該目錄下創(chuàng)建urls.py禁漓,并做如下配置
from?django.urls?import?path
from?.?import?views
urlpatterns?=?[
path('index/',views.index,name='index')#index/經(jīng)過上一級的路由映射,送到此處孵睬,index/會與path()的第一個參數(shù)匹配播歼,匹配成功,然后返回該app的views.index
]
step8:啟動項目
python manage.py runserver 0:8080
0是0.0.0.0的縮寫,允許任何ip訪問