91欧美超碰AV自拍|国产成年人性爱视频免费看|亚洲 日韩 欧美一厂二区入|人人看人人爽人人操aV|丝袜美腿视频一区二区在线看|人人操人人爽人人爱|婷婷五月天超碰|97色色欧美亚州A√|另类A√无码精品一级av|欧美特级日韩特级

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

在Django應(yīng)用程序開發(fā)中設(shè)計(jì)Django模板的方法

馬哥Linux運(yùn)維 ? 來源:Python運(yùn)維技術(shù) ? 作者:Python運(yùn)維技術(shù) ? 2021-07-29 15:44 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

在本文中,我將介紹在Django應(yīng)用程序開發(fā)中設(shè)計(jì)Django模板的方法。目的是保持Django應(yīng)用程序的UI部分井井有條,并避免重復(fù)編碼。Django在模板引擎中提供了各種機(jī)制來幫助我們實(shí)現(xiàn)這一目標(biāo)。在本教程中,我將說明如何使用Django內(nèi)置模板標(biāo)記塊,擴(kuò)展和包含來使模板易于維護(hù)。

準(zhǔn)備工作:

1、Python 3.6

2、Django 2.2

3、AdminLTE 3.0.5

我們目標(biāo)是將模板文件有效組織起來,避免重復(fù)的代碼引用,我們分四個(gè)步驟來實(shí)現(xiàn)。

步驟1/4:base.html

將模板分為多個(gè)部分,我們知道除了菜單和內(nèi)容外,其他所有內(nèi)容都是可重復(fù)的。我們將制作一個(gè)基本模板來容納那些常見的部分

在項(xiàng)目文件夾中創(chuàng)建一個(gè)文件夾模板。在其中創(chuàng)建一個(gè)base.html。將所有常見的片段添加到其中。只需復(fù)制并粘貼以下內(nèi)容,僅是load.html和index.html共享的一部分代碼。

{% load static %}

《!DOCTYPE html》

《!--

This is a starter template page. Use this page to start your new project from

scratch. This page gets rid of all links and provides the needed markup only.

--》

《html lang=“en”》

《head》

《meta charset=“utf-8”》

《meta name=“viewport” content=“width=device-width, initial-scale=1”》

《meta http-equiv=“x-ua-compatible” content=“ie=edge”》

《title》AdminLTE 3 | Starter《/title》

《!-- Font Awesome Icons --》

《link rel=“stylesheet” href=“{% static ‘plugins/fontawesome-free/css/all.min.css’ %}”》

《!-- Theme style --》

《link rel=“stylesheet” href=“{% static ‘dist/css/adminlte.min.css’ %}”》

《!-- Google Font: Source Sans Pro --》

《link href=“https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700” rel=“stylesheet”》

《/head》

《body class=“hold-transition sidebar-mini”》

《div class=“wrapper”》

《!-- Navbar --》

《nav class=“main-header navbar navbar-expand navbar-white navbar-light”》

《!-- Left navbar links --》

《ul class=“navbar-nav”》

《li class=“nav-item”》

《a class=“nav-link” data-widget=“pushmenu” href=“#” role=“button”》《i class=“fas fa-bars”》《/i》《/a》

《/li》

《li class=“nav-item d-none d-sm-inline-block”》

《a href=“index3.html” class=“nav-link”》Home《/a》

《/li》

《li class=“nav-item d-none d-sm-inline-block”》

《a href=“#” class=“nav-link”》Contact《/a》

《/li》

《/ul》

《!-- SEARCH FORM --》

《form class=“form-inline ml-3”》

《div class=“input-group input-group-sm”》

《input class=“form-control form-control-navbar” type=“search” placeholder=“Search” aria-label=“Search”》

《div class=“input-group-append”》

《button class=“btn btn-navbar” type=“submit”》

《i class=“fas fa-search”》《/i》

《/button》

《/div》

《/div》

《/form》

《!-- Right navbar links --》

《ul class=“navbar-nav ml-auto”》

《!-- Messages Dropdown Menu --》

《li class=“nav-item dropdown”》

《a class=“nav-link” data-toggle=“dropdown” href=“#”》

《i class=“far fa-comments”》《/i》

《span class=“badge badge-danger navbar-badge”》3《/span》

《/a》

《div class=“dropdown-menu dropdown-menu-lg dropdown-menu-right”》

《a href=“#” class=“dropdown-item”》

《!-- Message Start --》

《div class=“media”》

《img src=“{% static ‘dist/img/user1-128x128.jpg’ %}” alt=“User Avatar” class=“img-size-50 mr-3 img-circle”》

《div class=“media-body”》

《h3 class=“dropdown-item-title”》

Brad Diesel

《span class=“float-right text-sm text-danger”》《i class=“fas fa-star”》《/i》《/span》

《/h3》

《p class=“text-sm”》Call me whenever you can.。?!?p》

《p class=“text-sm text-muted”》《i class=“far fa-clock mr-1”》《/i》 4 Hours Ago《/p》

《/div》

《/div》

《!-- Message End --》

《/a》

《div class=“dropdown-divider”》《/div》

《a href=“#” class=“dropdown-item”》

《!-- Message Start --》

《div class=“media”》

《img src=“{% static ‘dist/img/user8-128x128.jpg’ %}” alt=“User Avatar” class=“img-size-50 img-circle mr-3”》

《div class=“media-body”》

《h3 class=“dropdown-item-title”》

John Pierce

《span class=“float-right text-sm text-muted”》《i class=“fas fa-star”》《/i》《/span》

《/h3》

《p class=“text-sm”》I got your message bro《/p》

《p class=“text-sm text-muted”》《i class=“far fa-clock mr-1”》《/i》 4 Hours Ago《/p》

《/div》

《/div》

《!-- Message End --》

《/a》

《div class=“dropdown-divider”》《/div》

《a href=“#” class=“dropdown-item”》

《!-- Message Start --》

《div class=“media”》

《img src=“{% static ‘dist/img/user3-128x128.jpg’ %}” alt=“User Avatar” class=“img-size-50 img-circle mr-3”》

《div class=“media-body”》

《h3 class=“dropdown-item-title”》

Nora Silvester

《span class=“float-right text-sm text-warning”》《i class=“fas fa-star”》《/i》《/span》

《/h3》

《p class=“text-sm”》The subject goes here《/p》

《p class=“text-sm text-muted”》《i class=“far fa-clock mr-1”》《/i》 4 Hours Ago《/p》

《/div》

《/div》

《!-- Message End --》

《/a》

《div class=“dropdown-divider”》《/div》

《a href=“#” class=“dropdown-item dropdown-footer”》See All Messages《/a》

《/div》

《/li》

《!-- Notifications Dropdown Menu --》

《li class=“nav-item dropdown”》

《a class=“nav-link” data-toggle=“dropdown” href=“#”》

《i class=“far fa-bell”》《/i》

《span class=“badge badge-warning navbar-badge”》15《/span》

《/a》

《div class=“dropdown-menu dropdown-menu-lg dropdown-menu-right”》

《span class=“dropdown-header”》15 Notifications《/span》

《div class=“dropdown-divider”》《/div》

《a href=“#” class=“dropdown-item”》

《i class=“fas fa-envelope mr-2”》《/i》 4 new messages

《span class=“float-right text-muted text-sm”》3 mins《/span》

《/a》

《div class=“dropdown-divider”》《/div》

《a href=“#” class=“dropdown-item”》

《i class=“fas fa-users mr-2”》《/i》 8 friend requests

《span class=“float-right text-muted text-sm”》12 hours《/span》

《/a》

《div class=“dropdown-divider”》《/div》

《a href=“#” class=“dropdown-item”》

《i class=“fas fa-file mr-2”》《/i》 3 new reports

《span class=“float-right text-muted text-sm”》2 days《/span》

《/a》

《div class=“dropdown-divider”》《/div》

《a href=“#” class=“dropdown-item dropdown-footer”》See All Notifications《/a》

《/div》

《/li》

《li class=“nav-item”》

《a class=“nav-link” data-widget=“control-sidebar” data-slide=“true” href=“#” role=“button”》《i

class=“fas fa-th-large”》《/i》《/a》

《/li》

《/ul》

《/nav》

《!-- /.navbar --》

《!-- Main Sidebar Container --》

《aside class=“main-sidebar sidebar-dark-primary elevation-4”》

《!-- Brand Logo --》

《a href=“{% url ‘home:landing’ %}” class=“brand-link”》

《img src=“{% static ‘dist/img/AdminLTELogo.png’ %}” alt=“AdminLTE Logo” class=“brand-image img-circle elevation-3”

style=“opacity: .8”》

《span class=“brand-text font-weight-light”》AdminLTE 3《/span》

《/a》

《!-- Sidebar --》

《div class=“sidebar”》

《!-- Sidebar user panel (optional) --》

《div class=“user-panel mt-3 pb-3 mb-3 d-flex”》

《div class=“image”》

《img src=“{% static ‘dist/img/user2-160x160.jpg’ %}” class=“img-circle elevation-2” alt=“User Image”》

《/div》

《div class=“info”》

《a href=“#” class=“d-block”》Alexander Pierce《/a》

《/div》

《/div》

《!-- Sidebar Menu --》

{% block sidebar %}{% endblock %}

《!-- /.sidebar-menu --》

《/div》

《!-- /.sidebar --》

《/aside》

《!-- Content Wrapper. Contains page content --》

{% block content_wrapper %}{% endblock %}

《!-- /.content-wrapper --》

《!-- Control Sidebar --》

《aside class=“control-sidebar control-sidebar-dark”》

《!-- Control sidebar content goes here --》

《div class=“p-3”》

《h5》Title《/h5》

《p》Sidebar content《/p》

《/div》

《/aside》

《!-- /.control-sidebar --》

《!-- Main Footer --》

《footer class=“main-footer”》

《!-- To the right --》

《div class=“float-right d-none d-sm-inline”》

Anything you want

《/div》

《!-- Default to the left --》

《strong》Copyright ? 2014-2019 《a href=“https://adminlte.io”》AdminLTE.io《/a》。《/strong》 All rights reserved.

《/footer》

《/div》

《!-- 。/wrapper --》

《!-- REQUIRED SCRIPTS --》

《!-- jQuery --》

《script src=“{% static ‘plugins/jquery/jquery.min.js’ %}”》《/script》

《!-- Bootstrap 4 --》

《script src=“{% static ‘plugins/bootstrap/js/bootstrap.bundle.min.js’ %}”》《/script》

《!-- AdminLTE App --》

《script src=“{% static ‘dist/js/adminlte.min.js’ %}”》《/script》

《/body》

《/html》

請注意,塊content_wrapper用于呈現(xiàn)每個(gè)頁面的自定義內(nèi)容。

步驟2/4:刪除冗余的通用代碼

由于我們在上一步中創(chuàng)建了base.html,因此不再需要將通用代碼保留在Landing.html和home.html中。我們應(yīng)該得到如下結(jié)果。

《div class=“content-wrapper”》

《!-- Content Header (Page header) --》

《div class=“content-header”》

《div class=“container-fluid”》

《div class=“row mb-2”》

《div class=“col-sm-6”》

《h1 class=“m-0 text-dark”》Polls Index Page《/h1》

《/div》《!-- /.col --》

《div class=“col-sm-6”》

《ol class=“breadcrumb float-sm-right”》

《li class=“breadcrumb-item”》《a href=“#”》Home《/a》《/li》

《li class=“breadcrumb-item active”》Polls《/li》

《/ol》

《/div》《!-- /.col --》

《/div》《!-- /.row --》

《/div》《!-- /.container-fluid --》

《/div》

《!-- /.content-header --》

《!-- Main content --》

《div class=“content”》

《div class=“container-fluid”》

《div class=“row”》

《div class=“col-lg-6”》

《div class=“card”》

《div class=“card-body”》

《h5 class=“card-title”》Card title《/h5》

《p class=“card-text”》

Some quick example text to build on the card title and make up the bulk of the card‘s

content.

《/p》

《a href=“#” class=“card-link”》Card link《/a》

《a href=“#” class=“card-link”》Another link《/a》

《/div》

《/div》

《div class=“card card-primary card-outline”》

《div class=“card-body”》

《h5 class=“card-title”》Card title《/h5》

《p class=“card-text”》

Some quick example text to build on the card title and make up the bulk of the card’s

content.

《/p》

《a href=“#” class=“card-link”》Card link《/a》

《a href=“#” class=“card-link”》Another link《/a》

《/div》

《/div》《!-- /.card --》

《!-- general form elements disabled --》

《div class=“card card-primary card-outline”》

《div class=“card-header”》

《h3 class=“card-title”》General Elements《/h3》

《/div》

《!-- /.card-header --》

《div class=“card-body”》

《form role=“form”》

《div class=“row”》

《div class=“col-sm-6”》

《!-- text input --》

《div class=“form-group”》

《label》Text《/label》

《input type=“text” class=“form-control” placeholder=“Enter 。。?!薄?/p>

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Text Disabled《/label》

《input type=“text” class=“form-control” placeholder=“Enter 。。?!?disabled》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- textarea --》

《div class=“form-group”》

《label》Textarea《/label》

《textarea class=“form-control” rows=“3” placeholder=“Enter 。。?!薄贰?textarea》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Textarea Disabled《/label》

《textarea class=“form-control” rows=“3” placeholder=“Enter 。。?!?disabled》《/textarea》

《/div》

《/div》

《/div》

《!-- input states --》

《div class=“form-group”》

《label class=“col-form-label” for=“inputSuccess”》《i class=“fas fa-check”》《/i》 Input with

success《/label》

《input type=“text” class=“form-control is-valid” id=“inputSuccess” placeholder=“Enter 。。?!薄?/p>

《/div》

《div class=“form-group”》

《label class=“col-form-label” for=“inputWarning”》《i class=“far fa-bell”》《/i》 Input with

warning《/label》

《input type=“text” class=“form-control is-warning” id=“inputWarning” placeholder=“Enter 。。?!薄?/p>

《/div》

《div class=“form-group”》

《label class=“col-form-label” for=“inputError”》《i class=“far fa-times-circle”》《/i》 Input with

error《/label》

《input type=“text” class=“form-control is-invalid” id=“inputError” placeholder=“Enter 。。?!薄?/p>

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- checkbox --》

《div class=“form-group”》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox”》

《label class=“form-check-label”》Checkbox《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox” checked》

《label class=“form-check-label”》Checkbox checked《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox” disabled》

《label class=“form-check-label”》Checkbox disabled《/label》

《/div》

《/div》

《/div》

《div class=“col-sm-6”》

《!-- radio --》

《div class=“form-group”》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” name=“radio1”》

《label class=“form-check-label”》Radio《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” name=“radio1” checked》

《label class=“form-check-label”》Radio checked《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” disabled》

《label class=“form-check-label”》Radio disabled《/label》

《/div》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- select --》

《div class=“form-group”》

《label》Select《/label》

《select class=“form-control”》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Select Disabled《/label》

《select class=“form-control” disabled》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- Select multiple--》

《div class=“form-group”》

《label》Select Multiple《/label》

《select multiple class=“form-control”》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Select Multiple Disabled《/label》

《select multiple class=“form-control” disabled》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《/div》

《/form》

《/div》

《!-- /.card-body --》

《/div》

《!-- /.card --》

《/div》

《!-- /.col-md-6 --》

《div class=“col-lg-6”》

《div class=“card”》

《div class=“card-header”》

《h5 class=“m-0”》Featured《/h5》

《/div》

《div class=“card-body”》

《h6 class=“card-title”》Special title treatment《/h6》

《p class=“card-text”》With supporting text below as a natural lead-in to additional content.《/p》

《a href=“#” class=“btn btn-primary”》Go somewhere《/a》

《/div》

《/div》

《div class=“card card-primary card-outline”》

《div class=“card-header”》

《h5 class=“m-0”》Featured《/h5》

《/div》

《div class=“card-body”》

《h6 class=“card-title”》Special title treatment《/h6》

《p class=“card-text”》With supporting text below as a natural lead-in to additional content.《/p》

《a href=“#” class=“btn btn-primary”》Go somewhere《/a》

《/div》

《/div》

《/div》

《!-- /.col-md-6 --》

《/div》

《!-- /.row --》

《/div》《!-- /.container-fluid --》

《/div》

《!-- /.content --》

《/div》

landing.html頁面代碼:

《div class=“content-wrapper”》

《!-- Content Header (Page header) --》

《div class=“content-header”》

《div class=“container-fluid”》

《div class=“row mb-2”》

《div class=“col-sm-6”》

《h1 class=“m-0 text-dark”》Home Landing Page《/h1》

《/div》《!-- /.col --》

《div class=“col-sm-6”》

《ol class=“breadcrumb float-sm-right”》

《li class=“breadcrumb-item”》《a href=“#”》Landing Page《/a》《/li》

《/ol》

《/div》《!-- /.col --》

《/div》《!-- /.row --》

《/div》《!-- /.container-fluid --》

《/div》

《!-- /.content-header --》

《!-- Main content --》

《div class=“content”》

《div class=“container-fluid”》

《div class=“row”》

《div class=“col-lg-6”》

《div class=“card”》

《div class=“card-body”》

《h5 class=“card-title”》Card title《/h5》

《p class=“card-text”》

Some quick example text to build on the card title and make up the bulk of the card‘s

content.

《/p》

《a href=“#” class=“card-link”》Card link《/a》

《a href=“#” class=“card-link”》Another link《/a》

《/div》

《/div》

《div class=“card card-primary card-outline”》

《div class=“card-body”》

《h5 class=“card-title”》Card title《/h5》

《p class=“card-text”》

Some quick example text to build on the card title and make up the bulk of the card’s

content.

《/p》

《a href=“#” class=“card-link”》Card link《/a》

《a href=“#” class=“card-link”》Another link《/a》

《/div》

《/div》《!-- /.card --》

《/div》

《div class=“col-md-6”》

《!-- general form elements disabled --》

《div class=“card card-warning”》

《div class=“card-header”》

《h3 class=“card-title”》General Elements《/h3》

《/div》

《!-- /.card-header --》

《div class=“card-body”》

《form role=“form”》

《div class=“row”》

《div class=“col-sm-6”》

《!-- text input --》

《div class=“form-group”》

《label》Text《/label》

《input type=“text” class=“form-control” placeholder=“Enter 。。。”》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Text Disabled《/label》

《input type=“text” class=“form-control” placeholder=“Enter 。。。” disabled》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- textarea --》

《div class=“form-group”》

《label》Textarea《/label》

《textarea class=“form-control” rows=“3” placeholder=“Enter 。。?!薄贰?textarea》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Textarea Disabled《/label》

《textarea class=“form-control” rows=“3” placeholder=“Enter 。。?!?disabled》《/textarea》

《/div》

《/div》

《/div》

《!-- input states --》

《div class=“form-group”》

《label class=“col-form-label” for=“inputSuccess”》《i class=“fas fa-check”》《/i》 Input with

success《/label》

《input type=“text” class=“form-control is-valid” id=“inputSuccess” placeholder=“Enter 。。。”》

《/div》

《div class=“form-group”》

《label class=“col-form-label” for=“inputWarning”》《i class=“far fa-bell”》《/i》 Input with

warning《/label》

《input type=“text” class=“form-control is-warning” id=“inputWarning” placeholder=“Enter 。。?!薄?/p>

《/div》

《div class=“form-group”》

《label class=“col-form-label” for=“inputError”》《i class=“far fa-times-circle”》《/i》 Input with

error《/label》

《input type=“text” class=“form-control is-invalid” id=“inputError” placeholder=“Enter 。。?!薄?/p>

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- checkbox --》

《div class=“form-group”》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox”》

《label class=“form-check-label”》Checkbox《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox” checked》

《label class=“form-check-label”》Checkbox checked《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox” disabled》

《label class=“form-check-label”》Checkbox disabled《/label》

《/div》

《/div》

《/div》

《div class=“col-sm-6”》

《!-- radio --》

《div class=“form-group”》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” name=“radio1”》

《label class=“form-check-label”》Radio《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” name=“radio1” checked》

《label class=“form-check-label”》Radio checked《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” disabled》

《label class=“form-check-label”》Radio disabled《/label》

《/div》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- select --》

《div class=“form-group”》

《label》Select《/label》

《select class=“form-control”》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Select Disabled《/label》

《select class=“form-control” disabled》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- Select multiple--》

《div class=“form-group”》

《label》Select Multiple《/label》

《select multiple class=“form-control”》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Select Multiple Disabled《/label》

《select multiple class=“form-control” disabled》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《/div》

《/form》

《/div》

《!-- /.card-body --》

《/div》

《!-- /.card --》

《/div》

《/div》

《/div》《!-- /.container-fluid --》

《/div》

《!-- /.content --》

《/div》

步驟3/4:繼承base.html

為了將base.html用作每個(gè)頁面的基礎(chǔ)模板,我們需要通過在模板的開頭使用{%extended‘base.html’%}來聲明base.html為“父”模板。最重要的是,不要忘記content_wrapper塊。將全部內(nèi)容包裝到該塊中。我們應(yīng)該得到如下結(jié)果。

landing.html:

{% extends ‘base.html’ %}

{% load static %}

{% block content_wrapper %}

《div class=“content-wrapper”》

。 。 。

《/div》

{% endblock %}

在index.html:

{% extends ‘base.html’ %}

{% load static %}

{% block content_wrapper %}

《div class=“content-wrapper”》

。 。 。

《/div》

{% endblock %}

步驟4/4:將常見的內(nèi)容單獨(dú)存放

現(xiàn)在我們可能會意識到,兩個(gè)模板中都存在相同的巨型形式。幾乎一半的代碼是它。由于此表單已在兩個(gè)模板中重復(fù)使用,因此我們將其維護(hù)在一個(gè)可以包含任何模板的地方。

在模板文件夾中創(chuàng)建一個(gè)文件夾advanced_forms。在advanced_forms文件夾中,創(chuàng)建如下的general_elements_form.html,代碼如下:

《form role=“form”》

《div class=“row”》

《div class=“col-sm-6”》

《!-- text input --》

《div class=“form-group”》

《label》Text《/label》

《input type=“text” class=“form-control” placeholder=“Enter 。。。”》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Text Disabled《/label》

《input type=“text” class=“form-control” placeholder=“Enter 。。。” disabled》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- textarea --》

《div class=“form-group”》

《label》Textarea《/label》

《textarea class=“form-control” rows=“3” placeholder=“Enter 。。。”》《/textarea》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Textarea Disabled《/label》

《textarea class=“form-control” rows=“3” placeholder=“Enter 。。?!?disabled》《/textarea》

《/div》

《/div》

《/div》

《!-- input states --》

《div class=“form-group”》

《label class=“col-form-label” for=“inputSuccess”》《i class=“fas fa-check”》《/i》 Input with

success《/label》

《input type=“text” class=“form-control is-valid” id=“inputSuccess” placeholder=“Enter 。。?!薄?/p>

《/div》

《div class=“form-group”》

《label class=“col-form-label” for=“inputWarning”》《i class=“far fa-bell”》《/i》 Input with

warning《/label》

《input type=“text” class=“form-control is-warning” id=“inputWarning” placeholder=“Enter 。。。”》

《/div》

《div class=“form-group”》

《label class=“col-form-label” for=“inputError”》《i class=“far fa-times-circle”》《/i》 Input with

error《/label》

《input type=“text” class=“form-control is-invalid” id=“inputError” placeholder=“Enter 。。?!薄?/p>

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- checkbox --》

《div class=“form-group”》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox”》

《label class=“form-check-label”》Checkbox《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox” checked》

《label class=“form-check-label”》Checkbox checked《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“checkbox” disabled》

《label class=“form-check-label”》Checkbox disabled《/label》

《/div》

《/div》

《/div》

《div class=“col-sm-6”》

《!-- radio --》

《div class=“form-group”》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” name=“radio1”》

《label class=“form-check-label”》Radio《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” name=“radio1” checked》

《label class=“form-check-label”》Radio checked《/label》

《/div》

《div class=“form-check”》

《input class=“form-check-input” type=“radio” disabled》

《label class=“form-check-label”》Radio disabled《/label》

《/div》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- select --》

《div class=“form-group”》

《label》Select《/label》

《select class=“form-control”》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Select Disabled《/label》

《select class=“form-control” disabled》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《/div》

《div class=“row”》

《div class=“col-sm-6”》

《!-- Select multiple--》

《div class=“form-group”》

《label》Select Multiple《/label》

《select multiple class=“form-control”》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《div class=“col-sm-6”》

《div class=“form-group”》

《label》Select Multiple Disabled《/label》

《select multiple class=“form-control” disabled》

《option》option 1《/option》

《option》option 2《/option》

《option》option 3《/option》

《option》option 4《/option》

《option》option 5《/option》

《/select》

《/div》

《/div》

《/div》

《/form》

刪除Landing.html和index.html中的多余表單代碼。使用{% include ‘a(chǎn)dvanced_forms/general_elements_form.html’ %}包含表單。設(shè)置好之后,這就是最終結(jié)果。

index.html:

{% extends ‘base.html’ %}

{% load static %}

{% block content_wrapper %}

《div class=“content-wrapper”》

《!-- Content Header (Page header) --》

《div class=“content-header”》

《div class=“container-fluid”》

《div class=“row mb-2”》

《div class=“col-sm-6”》

《h1 class=“m-0 text-dark”》Polls Index Page《/h1》

《/div》《!-- /.col --》

《div class=“col-sm-6”》

《ol class=“breadcrumb float-sm-right”》

《li class=“breadcrumb-item”》《a href=“#”》Home《/a》《/li》

《li class=“breadcrumb-item active”》Polls《/li》

《/ol》

《/div》《!-- /.col --》

《/div》《!-- /.row --》

《/div》《!-- /.container-fluid --》

《/div》

《!-- /.content-header --》

《!-- Main content --》

《div class=“content”》

《div class=“container-fluid”》

《div class=“row”》

《div class=“col-lg-6”》

《div class=“card”》

《div class=“card-body”》

《h5 class=“card-title”》Card title《/h5》

《p class=“card-text”》

Some quick example text to build on the card title and make up the bulk of the card‘s

content.

《/p》

《a href=“#” class=“card-link”》Card link《/a》

《a href=“#” class=“card-link”》Another link《/a》

《/div》

《/div》

《div class=“card card-primary card-outline”》

《div class=“card-body”》

《h5 class=“card-title”》Card title《/h5》

《p class=“card-text”》

Some quick example text to build on the card title and make up the bulk of the card’s

content.

《/p》

《a href=“#” class=“card-link”》Card link《/a》

《a href=“#” class=“card-link”》Another link《/a》

《/div》

《/div》《!-- /.card --》

《!-- general form elements disabled --》

《div class=“card card-primary card-outline”》

《div class=“card-header”》

《h3 class=“card-title”》General Elements《/h3》

《/div》

《!-- /.card-header --》

《div class=“card-body”》

{% include ‘a(chǎn)dvanced_forms/general_elements_form.html’ %}

《/div》

《!-- /.card-body --》

《/div》

《!-- /.card --》

《/div》

《!-- /.col-md-6 --》

《div class=“col-lg-6”》

《div class=“card”》

《div class=“card-header”》

《h5 class=“m-0”》Featured《/h5》

《/div》

《div class=“card-body”》

《h6 class=“card-title”》Special title treatment《/h6》

《p class=“card-text”》With supporting text below as a natural lead-in to additional content.《/p》

《a href=“#” class=“btn btn-primary”》Go somewhere《/a》

《/div》

《/div》

《div class=“card card-primary card-outline”》

《div class=“card-header”》

《h5 class=“m-0”》Featured《/h5》

《/div》

《div class=“card-body”》

《h6 class=“card-title”》Special title treatment《/h6》

《p class=“card-text”》With supporting text below as a natural lead-in to additional content.《/p》

《a href=“#” class=“btn btn-primary”》Go somewhere《/a》

《/div》

《/div》

《/div》

《!-- /.col-md-6 --》

《/div》

《!-- /.row --》

《/div》《!-- /.container-fluid --》

《/div》

《!-- /.content --》

《/div》

{% endblock %}

loading.html:

{% extends ‘base.html’ %}

{% load static %}

{% block content_wrapper %}

《div class=“content-wrapper”》

《!-- Content Header (Page header) --》

《div class=“content-header”》

《div class=“container-fluid”》

《div class=“row mb-2”》

《div class=“col-sm-6”》

《h1 class=“m-0 text-dark”》Home Landing Page《/h1》

《/div》《!-- /.col --》

《div class=“col-sm-6”》

《ol class=“breadcrumb float-sm-right”》

《li class=“breadcrumb-item”》《a href=“#”》Landing Page《/a》《/li》

《/ol》

《/div》《!-- /.col --》

《/div》《!-- /.row --》

《/div》《!-- /.container-fluid --》

《/div》

《!-- /.content-header --》

《!-- Main content --》

《div class=“content”》

《div class=“container-fluid”》

《div class=“row”》

《div class=“col-lg-6”》

《div class=“card”》

《div class=“card-body”》

《h5 class=“card-title”》Card title《/h5》

《p class=“card-text”》

Some quick example text to build on the card title and make up the bulk of the card‘s

content.

《/p》

《a href=“#” class=“card-link”》Card link《/a》

《a href=“#” class=“card-link”》Another link《/a》

《/div》

《/div》

《div class=“card card-primary card-outline”》

《div class=“card-body”》

《h5 class=“card-title”》Card title《/h5》

《p class=“card-text”》

Some quick example text to build on the card title and make up the bulk of the card’s

content.

《/p》

《a href=“#” class=“card-link”》Card link《/a》

《a href=“#” class=“card-link”》Another link《/a》

《/div》

《/div》《!-- /.card --》

《/div》

《div class=“col-md-6”》

《!-- general form elements disabled --》

《div class=“card card-warning”》

《div class=“card-header”》

《h3 class=“card-title”》General Elements《/h3》

《/div》

《!-- /.card-header --》

《div class=“card-body”》

{% include ‘a(chǎn)dvanced_forms/general_elements_form.html’ %}

《/div》

《!-- /.card-body --》

《/div》

《!-- /.card --》

《/div》

《/div》

《/div》《!-- /.container-fluid --》

《/div》

《!-- /.content --》

《/div》

{% endblock %}

現(xiàn)在,讓我們再次重新啟動項(xiàng)目。屏幕上沒有任何變化。但是從整體結(jié)構(gòu)上,項(xiàng)目變得更易于維護(hù)。努力將是值得的(笑臉)這是我們完成增強(qiáng)后的文件架構(gòu)的目錄結(jié)構(gòu)。我用黃色突出顯示了模板。

寫在最后,模板是Web應(yīng)用程序中的核心部分之一。不要寫重復(fù)代碼,我認(rèn)為該原則適用于前端和后端開發(fā),這樣我們才可以制作出可伸縮的應(yīng)用程序。

文章轉(zhuǎn)載:Python運(yùn)維技術(shù)

(版權(quán)歸原作者所有,侵刪)

編輯:jq

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • Django
    +關(guān)注

    關(guān)注

    0

    文章

    45

    瀏覽量

    10873

原文標(biāo)題:如何設(shè)計(jì)組織Django模板?

文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點(diǎn)推薦

    瑞薩RA-Eco-RA4M2開發(fā)板基本模板搭建與LED指南

    瑞薩“RA MCU眾測寶典”環(huán)境搭建專題再添硬核實(shí)操!這次將解鎖“基本模板搭建與LED”技能,加入簡易調(diào)度器實(shí)現(xiàn)多任務(wù)管理,一步步搞定“能直接落地”的開發(fā)模板,不管是入門練手還是項(xiàng)目開發(fā)
    的頭像 發(fā)表于 02-24 14:43 ?5314次閱讀
    瑞薩RA-Eco-RA4M2<b class='flag-5'>開發(fā)</b>板基本<b class='flag-5'>模板</b>搭建與LED指南

    深入了解API:詳解應(yīng)用程序接口的作用和原理

    引言:現(xiàn)代軟件開發(fā)領(lǐng)域中, API(Application Programming Interface,應(yīng)用程序接口)扮演著至關(guān)重要的角色。無論是Web
    的頭像 發(fā)表于 11-03 09:37 ?355次閱讀

    程序加載過程遇到的問題及其解決方法

    。重新啟動Nuclei Studio,該問題解決。 (2)遇到的問題2:下載程序顯示未連接到開發(fā)板,報(bào)錯界面如下圖所示。 解決方法配套文檔
    發(fā)表于 10-30 07:59

    PYQT 應(yīng)用程序框架及開發(fā)工具

    大家好,本團(tuán)隊(duì)此次分享的內(nèi)容為開發(fā)過程中使用到的PYQT 應(yīng)用程序框架及開發(fā)工具。 pYqt 是一個(gè)多平臺的 python 圖形用戶界面應(yīng)用程序框架,由于其面向?qū)ο蟆? 易擴(kuò)展(可
    發(fā)表于 10-29 07:15

    NucleiStudio下載程序到FPGA的E203上

    使用NucleiStudio開發(fā)應(yīng)用程序,并將應(yīng)用程序的代碼下載到FPGA上。這里,以helloworld為例,介紹該過程遇到的問題以及解決方法。 1、新建Nuclei RISC-V
    發(fā)表于 10-20 09:24

    店鋪裝修模板同步接口技術(shù)解析

    ? ?電商平臺開發(fā),店鋪裝修模板的跨系統(tǒng)同步是核心需求。本文從接口設(shè)計(jì)、數(shù)據(jù)結(jié)構(gòu)和實(shí)現(xiàn)邏輯三個(gè)維度進(jìn)行技術(shù)拆解。 一、接口設(shè)計(jì)規(guī)范 基礎(chǔ)參數(shù) 請求
    的頭像 發(fā)表于 10-17 15:24 ?301次閱讀
    店鋪裝修<b class='flag-5'>模板</b>同步接口技術(shù)解析

    學(xué)生適合使用的SOLIDWORKS 云應(yīng)用程序

    隨著科技的不斷發(fā)展,計(jì)算機(jī)輔助設(shè)計(jì)(CAD)技術(shù)已經(jīng)成為現(xiàn)代工程教育的重要組成部分。SOLIDWORKS作為一款CAD軟件,其教育版云應(yīng)用程序為學(xué)生提供了強(qiáng)大而靈活的設(shè)計(jì)平臺。本文將探討
    的頭像 發(fā)表于 09-15 10:39 ?775次閱讀
    學(xué)生適合使用的SOLIDWORKS 云<b class='flag-5'>應(yīng)用程序</b>

    嵌入式軟件開發(fā)常用的軟件有哪些?

    出一臺電腦,虛擬機(jī)上云心Linux系統(tǒng) 7. QT Creator Qt Creator是一個(gè)跨平臺的集成開發(fā)環(huán)境(?IDE)?,?專門用于開發(fā)Qt應(yīng)用程序。?它提供了從創(chuàng)建項(xiàng)目
    發(fā)表于 07-03 17:06

    CYBT-343026-01能否使用 HFP 和 AVRCP 制作應(yīng)用程序?

    我們計(jì)劃使用 CYBT-343026-01 制作使用 HFP 和 AVRCP 的應(yīng)用程序。 可以使用 CYBT-343026-01 制作使用 HFP 和 AVRCP 的應(yīng)用程序嗎? 根據(jù) QDID
    發(fā)表于 07-01 08:29

    基于LockAI視覺識別模塊:C++多模板匹配

    模板匹配是一種圖像同時(shí)尋找多個(gè)模板的技術(shù)。通過對每個(gè)模板逐一進(jìn)行匹配,找到與輸入圖像最相似的區(qū)域,并標(biāo)記出匹配度最高的結(jié)果。本實(shí)驗(yàn)提供
    發(fā)表于 05-14 15:00

    使用C++的CyAPI編寫的應(yīng)用程序上遇到了問題,求解決

    使用 C++ 的 CyAPI 編寫的應(yīng)用程序上遇到了問題。 我將 XferData() 方法與其他所有端點(diǎn)類型一起使用,沒有遇到任何問題。 但是,將其與 Endpoint0 一起
    發(fā)表于 05-13 06:11

    請問SX3實(shí)用程序提供的模板只能在開發(fā)板上使用嗎?

    SX3 實(shí)用程序提供的模板只能在開發(fā)板上使用嗎?
    發(fā)表于 05-09 06:47

    MCP:連接AI與應(yīng)用程序的開放標(biāo)準(zhǔn)!

    。 第二種方式開發(fā)者需要為AI大模型編寫與具體應(yīng)用程序協(xié)作的訪問代碼,例如,開發(fā)者希望DeepSeek-R1與Gmail協(xié)作,就需要手
    的頭像 發(fā)表于 03-21 18:21 ?2199次閱讀
    MCP:連接AI與<b class='flag-5'>應(yīng)用程序</b>的開放標(biāo)準(zhǔn)!

    如何部署OpenVINO?工具套件應(yīng)用程序?

    編寫代碼并測試 OpenVINO? 工具套件應(yīng)用程序后,必須將應(yīng)用程序安裝或部署到生產(chǎn)環(huán)境的目標(biāo)設(shè)備。 OpenVINO?部署管理器指南包含有關(guān)如何輕松使用部署管理器將應(yīng)用程序打包并
    發(fā)表于 03-06 08:23

    請問OpenVINO?工具套件的驗(yàn)證應(yīng)用程序是什么?

    OpenVINO?工具套件的驗(yàn)證應(yīng)用程序是什么?
    發(fā)表于 03-06 06:54