Python集成Django開發框架後,能夠經過在cmd命令提示符下創建工程,工程名爲learn_modelsjavascript
django-admin.py startproject learn_models
再進入到learn_models裏面,新建一個app項目css
cd learn_models python manage.py startapp learn
此時目錄的結構有這些文件html
C:\USERS\SHILEIDING\LEARN_MODELS │ manage.py │ ├─learn │ │ admin.py │ │ models.py │ │ tests.py │ │ views.py │ │ __init__.py │ │ │ └─migrations │ __init__.py │ └─learn_models settings.py settings.pyc urls.py wsgi.py __init__.py __init__.pyc
再去官網下載最新的Bootstrap3框架文件 http://getbootstrap.com/getting-started/#download 下載的文件夾能夠看出有css、fonts、js三個(功能至關大),這就是Bootstrap 3的所有,如下就要在剛新建的Django工程集合Bootstrap3,進入learn_models目錄,新建一個static文件夾,再在static裏面新建一個bootstrap文件夾,將下載的三個文件夾放進去。前端
回到learn_models目錄,進入learn目錄裏,新建一templates文件夾,裏面存放Bootstrap的html界面,如此處新建一文件test.html,要引用Bootstrap 和jQuery等相關庫,這裏重點是定位存放的static文件html5
<!DOCTYPE html> {% load staticfiles %} <html> <head lang="en"> <meta charset="UTF-8"> <!-- 引入jQuery --> <script src="http://apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <script src="http://code.jquery.com/jquery-latest.js"></script> <!-- 引入 Bootstrap --> <link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet" type="text/css"> <link href="{% static 'bootstrap/css/bootstrap.css' %}" rel="stylesheet" type="text/css"> <script type="text/javascript" src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script> <script type="text/javascript" src="{% static 'bootstrap/js/bootstrap.js' %}"></script> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> <![endif]--> <title>數據展現平臺</title> </head> <body> <!-- bootstrap 特性容器 --> <div class="container"> <h1>Hello, world! </h1> </div> </body> </html>
文件開頭的 {% load staticfiles %}就是加載static目錄,爲了找到static目錄,須要稍微修改下".../learn_models/learn_models/settings.py"中的配置,主要有兩塊修改java
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', #註冊新建的app 'learn', )
INSTALLED_APPS中添加新建的app,而後配置static相關
STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )
將static目錄放在 STATICFILES_DIRS 中,這樣就能夠load到咱們剛下載的bootstrap 了,bootstrap依賴於jQuery庫,因此必定要添加,咱們這裏是直接引用的,若是有下載版本只需放在static裏再引用就行。python
這時前端html已經可使用相關bootstrap屬性了,但如何經過Django 的http協議訪問呢?這就是Django傳奇的MVC模型了,剛剛的templates文件夾就是表現層,展現給用戶看的前端,views.py負責處理業務邏輯層,處理請求和返回請求,models.py負責數據存取層,處理數據庫的相關屬性。前端發出的GET或POST請求要經過urls.py映射到views的相關方法中,因此要在urls.py中配置映射關係,這裏假設請求路徑爲 http://127.0.0.1:8000/test/ 則配置爲jquery
urlpatterns = [ url(r'^admin/', include(admin.site.urls)), #前面是正則表達式 url(r'^test/','learn.views.test',name='test'), ]
映射到對應的views.py中,這裏簡單實現test方法,在views.py中添加便可正則表達式
#Bootstrap 測試 def test(request): return render(request, 'test.html')
當瀏覽器發出test請求後,先經過urls映射到views中的test方法,處理邏輯後推到前端test.html中顯示,html顯示的內容能夠利用下載的bootstrap渲染。數據庫
在cmd中cd到 learn_models目錄下 ,此時的目錄結構以下
C:\USERS\SHILEIDING\LEARN_MODELS │ manage.py │ ├─learn │ │ admin.py │ │ models.py │ │ tests.py │ │ views.py │ │ __init__.py │ │ │ ├─migrations │ │ __init__.py │ │ │ └─templates │ test.html │ ├─learn_models │ settings.py │ settings.pyc │ urls.py │ wsgi.py │ __init__.py │ __init__.pyc │ └─static └─bootstrap ├─css │ bootstrap-theme.css │ bootstrap-theme.css.map │ bootstrap-theme.min.css │ bootstrap.css │ bootstrap.css.map │ bootstrap.min.css │ ├─fonts │ glyphicons-halflings-regular.eot │ glyphicons-halflings-regular.svg │ glyphicons-halflings-regular.ttf │ glyphicons-halflings-regular.woff │ glyphicons-halflings-regular.woff2 │ └─js bootstrap.js bootstrap.min.js npm.js
能夠看到有manage.py,這正是運行的管理器,先同步數據庫,而後運行工程
#同步數據庫 python manage.py makemigrations python manage.py migrate #運行工程 python manage.py runserver
而後打開 http://127.0.0.1:8000/test/ 出如今偏中間的hello world 代表整合成功