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

  • 設(shè)置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),
]

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

from django.http import HttpResponse



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

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

def courseinfo(request):
    return HttpResponse("課程信息")
  • 設(shè)置app內(nèi)頁(yè)面路由
    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

  • 在項(xiàng)目根目錄下建立文件夾templates,并創(chuàng)建所需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',
            ],
        },
    },
]
  • 在根目錄下建立靜態(tài)資源文件夾static
?  proMana tree static -d
static
├── css
├── fonts
└── js
  • 在Django中引用Bootstrap模版

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

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

<!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個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! -->
    <title>Bootstrap 101 Template</title>

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

    <!-- HTML5 shim 和 Respond.js 是為了讓 IE8 支持 HTML5 元素和媒體查詢(xún)(media queries)功能 -->
    <!-- 警告:通過(guò) file:// 協(xié)議(就是直接將 html 頁(yè)面拖拽到瀏覽器中)訪(fǎng)問(wèn)頁(yè)面時(shí) 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 插件都依賴(lài) jQuery,所以必須放在前邊) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <!-- 加載 Bootstrap 的所有 JavaScript 插件。你也可以根據(jù)需要只加載單個(gè)插件。 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容