Django|路由與模板|URLconf &Templates

python:3.6.5
django:2.0.5

  • 文件目錄
?  proMana tree -d
.
├── appMana
│   ├── migrations
│   │   └── __pycache__
│   └── __pycache__
├── proMana
│   └── __pycache__
├── static
│   ├── css
│   ├── fonts
│   └── js
└── templates
    └── appMana


URL Configuration

  • 設置project路由
    proMana/urls
"""proMana URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include,path

urlpatterns = [
    path('',include('appMana.urls')),
    path('admin/', admin.site.urls),
]

  • 編寫view文件appMana/views.py
from django.shortcuts import render

from django.http import HttpResponse



# Create your views here.
def index(request):
    return HttpResponse("信息管理系統")

def stuinfo(request):
    #return HttpResponse("學生信息")
    return render(request,'appMana/stuinfo.html')
def scinfo(request):
    return HttpResponse("選課信息")

def courseinfo(request):
    return HttpResponse("課程信息")
  • 設置app內頁面路由
    appMana/urls
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('index/',views.index),
    path('stuinfo/',views.stuinfo),
    path('scinfo/',views.scinfo),
    path('courseinfo/',views.courseinfo)
]


在模板中使用Bootstrap

  • 在項目根目錄下建立文件夾templates,并創建所需html
?  proMana tree templates
templates
├── appMana
│   └── stuinfo.html
└── base.html

1 directory, 2 files
  • 修改proMana/settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/templates"], #修改這一條
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
  • 在根目錄下建立靜態資源文件夾static
?  proMana tree static -d
static
├── css
├── fonts
└── js
  • 在Django中引用Bootstrap模版

1、首先下載bootsrtap代碼(http://v3.bootcss.com/getting-started/#download),并將下載后的文件放在project下新創建的static目錄下。下載jquery(https://jquery.com/download/)放在static/js/目錄下。

2、下載合適的bootstrap模版樣式(http://v3.bootcss.com/getting-started/),下載的文件包含一個html和一個目錄。
實例模板:

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標簽*必須*放在最前面,任何其他內容都*必須*跟隨其后! -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link  rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是為了讓 IE8 支持 HTML5 元素和媒體查詢(media queries)功能 -->
    <!-- 警告:通過 file:// 協議(就是直接將 html 頁面拖拽到瀏覽器中)訪問頁面時 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <h1>你好,世界!</h1>

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依賴 jQuery,所以必須放在前邊) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <!-- 加載 Bootstrap 的所有 JavaScript 插件。你也可以根據需要只加載單個插件。 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </body>
</html>
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容