Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
字母索引
A 開頭B 開頭C 開頭D 開頭E 開頭F 開頭G 開頭H 開頭I 開頭J 開頭K 開頭L 開頭M 開頭N 開頭O 開頭P 開頭Q 開頭R 開頭S 開頭T 開頭U 開頭V 開頭W 開頭X 開頭Y 開頭Z 開頭專業名詞
A
英文譯法 1譯法 2譯法 3a block of pointers一塊指針一組指針abbreviation縮略語abstract抽象的abstract syntax tree, AST抽象語法樹abstraction抽象abstraction barrier抽象屏障抽象阻礙abstraction of function calls函數調用抽象access訪問存取access function訪問函數存取函數accumulator累加器activate激活ad hoc專設adapter適配器address地址algebraic data type代數數據類型algorithm算法alias別名allocate分配配置alternative備選amortized analysis平攤分析anaphoric指代annotation注解anonymous function匿名函數antecedent前提前件先決條件append追加拼接application應用應用程序application framework應用框架application program interface, API應用程序編程接口application service provider, ASP應用程序服務提供商applicative應用序argument參數自變量實際參數/實參arithmetic算術array數組artificial intelligence, AI人工智能assemble組合assembly匯編assignment賦值assignment operator賦值操作符associated關聯的association list, alist關聯列表atom原子atomic原子的atomic value原子型值attribute屬性特性augmented擴充automatic memory management自動內存管理automatically infer自動推導autometa theory自動機理論auxiliary輔助
B
英文譯法 1譯法 2譯法 3backquote反引用backtrace回溯backward compatible向下兼容bandwidth帶寬base case基本情形base class基類Bayes' theorem貝葉斯定理best viable function最佳可行函式最佳可行函數Bezier curve貝塞爾曲線bignum大數binary operator二元操作符binary search二分查找二分搜索二叉搜索binary search tree二叉搜索樹binary tree二叉樹binding綁定binding vector綁定向量bit位比特bit manipulation位操作black box abstraction黑箱抽象block塊區塊block structure塊結構區塊結構block name代碼塊名字Blub paradoxBlub 困境body體主體boilerplate公式化樣板bookkeeping簿記boolean布爾border邊框bottom-up design自底向上的設計bottom-up programming自底向上編程bound邊界bounds checking邊界檢查box notation箱子表示法brace花括弧花括號bracket方括弧方括號branch分支跳轉breadth-first廣度優先breadth-first search, BFS廣度優先搜索breakpoint斷點brevity簡潔buffer緩沖區buffer overflow attack緩沖區溢出攻擊bug臭蟲building創建built-in內置byte字節bytecode字節碼
C
英文譯法 1譯法 2譯法 3cache緩存call調用callback回調CamelCase駝峰式大小寫candidate function候選函數capture捕捉case分支character字符checksum校驗和child class子類choke point滯塞點chunk塊circular definition循環定義clarity清晰class類類別class declaration類聲明class library類庫client客戶客戶端clipboard剪貼板clone克隆closed world assumption封閉世界假定closure閉包clutter雜亂code代碼code bloat代碼膨脹collection收集器復合類型column行欄column-major order行主序comma逗號command-line命令行command-line interface, CLI命令行界面Common Lisp Object System, CLOSCommon Lisp 對象系統Common Gateway Interface, CGI通用網關接口compatible兼容compilation編譯compilation parameter編譯參數compile編譯compile inline內聯編譯compile time編譯期compiled form編譯后的形式compiler編譯器complex復雜complexity復雜度compliment補集component組件composability可組合性composition組合組合函數compound value復合數據復合值compression壓縮computation計算computer計算機concatenation串接concept概念concrete具體concurrency并發concurrent并發conditional條件式conditional variable條件變量configuration配置connection連接cons構造cons cell構元cons 單元consequent結果推論consistent一致性constant常量constraint約束constraint programming約束式編程container容器content-based filtering基于內容的過濾context上下文語境環境continuation延續性continuous integration, CI持續集成control控件cooperative multitasking協作式多任務copy拷貝corollary推論coroutine協程corruption程序崩潰crash崩潰create創建crystallize固化curly括弧狀的curried柯里的currying柯里化cursor光標curvy卷曲的cycle周期
D
英文譯法 1譯法 2譯法 3dangling pointer迷途指針野指針Defense Advanced Research Projects Agency, DARPA美國國防部高級研究計劃局data數據data structure數據結構data type數據類型data-driven數據驅動database數據庫database schema數據庫模式datagram數據報文dead lock死鎖debug調試debugger調試器debugging調試declaration聲明declaration forms聲明形式declarative聲明式說明式declarative knowledge聲明式知識說明式知識declarative programming聲明式編程說明式編程declarativeness可聲明性declaring聲明deconstruction解構deduction推導推斷default缺省默認defer推遲deficiency缺陷不足define定義definition定義delegate委托delegationdellocate釋放demarshal散集deprecated廢棄depth-first深度優先depth-first search, BFS深度優先搜索derived派生derived class派生類design pattern設計模式designator指示符destructive破壞性的destructive function破壞性函數destructuring解構device driver硬件驅動程序dimensions維度directive指令directive指示符directory目錄disk盤dispatch分派派發distributed computing分布式計算DLL hellDLL 地獄document文檔dotted list點狀列表dotted-pair notation帶點尾部表示法帶點尾部記法duplicate復本dynamic binding動態綁定dynamic extent動態范圍dynamic languages動態語言dynamic scope動態作用域dynamic type動態類型
E
英文譯法 1譯法 2譯法 3effect效果efficiency效率efficient高效elaborateelucidatingembedded language嵌入式語言emulate仿真encapsulation封裝enum枚舉enumeration type枚舉類型enumrators枚舉器environment環境equal相等equality相等性equation方程equivalence等價性error message錯誤信息error-checking錯誤檢查escaped逃脫溢出escape character轉義字符evaluate求值評估evaluation求值event事件event driven事件驅動exception異常exception handling異常處理exception specification異常規范exit退出expendable可擴展的explicit顯式exploratory programming探索式編程export導出引出expression表達式expressive power表達能力extensibility可擴展性extent范圍程度external representation外部表示法extreme programming極限編程
F
英文譯法 1譯法 2譯法 3factorial階乘family(類型的)系feasible可行的feature特色field字段欄位file文件file handle文件句柄fill pointer填充指針fineo-grained細粒度firmware固件first-class第一類的第一級的一等的first-class function第一級函數第一類函數一等函數first-class object第一類的對象第一級的對象一等公民fixed-point不動點fixnum定長數定點數flag標記flash閃存flexibility靈活性floating-point浮點數floating-point notation浮點數表示法flush刷新fold折疊font字體force迫使form形式form表單formal parameter形參formal relation形式關系forward轉發forward referencesfractal分形fractions派系framework框架freeware自由軟件function函數function literal函數字面常量function object函數對象functional arguments函數型參數functional programming函數式編程functionality功能性
G
英文譯法 1譯法 2譯法 3game游戲garbage垃圾garbage collection垃圾回收garbage collector垃圾回收器generalized泛化generalized variable廣義變量generate生成generator生成器generic通用的泛化的generic algorithm通用算法泛型算法generic function通用函數generic programming通用編程泛型編程genrative programming生產式編程global全局的global declaration全局聲明glue program膠水程序goto跳轉graphical user interface, GUI圖形用戶界面greatest common divisor最大公因數Greenspun's tenth rule格林斯潘第十定律
H
英文譯法 1譯法 2譯法 3hack破解hacker黑客handle處理器處理程序句柄hard disk硬盤hard-wirehardware硬件hash tables哈希表散列表header頭部header file頭文件heap堆helper輔助函數輔助方法heuristic啟發式high-order高階higher-order function高階函數higher-order procedure高階過程hyperlink超鏈接HyperText Markup Language, HTML超文本標記語言HyperText Transfer Protocol, HTTP超文本傳輸協議
I
英文譯法 1譯法 2譯法 3identical一致identifier標識符identity同一性ill type類型不正確illusion錯覺imperative命令式imperative programming命令式編程implement實現implementation實現implicit隱式import導入incremental testing增量測試indent縮排縮進indentation縮排縮進indented縮排縮進indention縮排縮進infer推導infinite loop無限循環infinite recursion無限遞歸infinite precision無限精度infix中序information信息information technology, IT信息技術inheritance繼承initialization初始化initialize初始化inline內聯inline expansion內聯展開inner class內嵌類inner loop內層循環input輸入instances實例instantiate實例化instructive教學性的instrument記錄儀integer整數integrate集成interactive language交互式語言interactive programming environment交互式編程環境interactive testing交互式測試interacts交互interface接口intermediate form過渡形式中間形式internal內部internet互聯網因特網interpolation插值interpret解釋interpreter解釋器interrupt中止中斷intersection交集inter-process communication, IPC進程間通信invariants約束條件invoke調用item項iterate迭代iteration迭代的iterative迭代的iterator迭代器
J
英文譯法 1譯法 2譯法 3jagged鋸齒狀的job control language, JCL作業控制語言judicious明智的
K
英文譯法 1譯法 2譯法 3kernel核心kernel language核心語言keyword argument關鍵字參數keywords關鍵字kludge蹩腳
L
英文譯法 1譯法 2譯法 3lambda calculuslambda 演算larval startup雛形創業公司laser激光latitudelayout版型lazy惰性lazy evaluation惰性求值legacy software歷史遺留軟件leverage杠桿(動詞)利用lexical詞法的lexical analysis詞法分析lexical closure詞法閉包lexical scope詞法作用域Language For Smart People, LFSP聰明人的語言library庫函數庫函式庫lifetime生命期linear iteration線性迭代linear recursion線性遞歸link鏈接連接linker連接器list列表list operation列表操作literal字面literal constant字面常量literal representation字面量load裝載加載loader裝載器加載器local局部的局域的local declarations局部聲明local function局部函數局域函數local variable局部變量局域變量locality局部性loop循環lvalue左值
M
英文譯法 1譯法 2譯法 3machine instruction機器指令machine language機器語言machine language code機器語言代碼machine learning機器學習macro宏mailing list郵件列表mainframes大型機maintain維護manifest typing顯式類型manipulator操縱器mapping映射mapping functions映射函數marshal列集math envy對數學家的妒忌member成員memorizing記憶化memory內存memory allocation內存分配memory leaks內存泄漏menu菜單message消息message-passing消息傳遞meta-元-meta-programming元編程metacircular元循環method方法method combination方法組合方法組合機制micro微middleware中間件migration(數據庫)遷移minimal network最小網絡mirror鏡射mismatch type類型不匹配model模型modem調制解調器modifier修飾符modularity模塊性module模塊monad單子monkey patch猴子補丁monomorphic type language單型語言Moore's law摩爾定律mouse鼠標multi-task多任務multiple values多值mutable可變的mutex互斥鎖Multiple Virtual Storage, MVS多重虛擬存儲
N
英文譯法 1譯法 2譯法 3namespace命名空間native本地的native code本地碼natural language自然語言natural language processing自然語言處理nested嵌套nested class嵌套類network網絡newline換行新行non-deterministic choice非確定性選擇non-strict非嚴格non-strict evaluation非嚴格求值nondeclarativenondestructive version非破壞性的版本number crunching數字密集運算
O
英文譯法 1譯法 2譯法 3object對象object code目標代碼object-oriented面向對象object-oriented programming面向對象編程Occam's razor奧卡姆剃刀原則on the fly運行中執行時online在線open source開放源碼operand操作對象operating system, OS操作系統operation操作operator操作符optimization優化optimization of tail calls尾調用優化option選項optional可選的選擇性的optional argument選擇性參數ordinary常規的orthogonality正交性overflow溢出overhead額外開銷overload重載override覆寫
P
英文譯法 1譯法 2譯法 3package包pair點對palindrome回文paradigm范式parallel并行parallel computer并行計算機param參數parameter參數形式參數/形參paren-matching括號匹配parent class父類parentheses括號Parkinson's law帕金森法則parse解析parse tree解析樹分析樹parser解析器partial application部分應用partial applied分步代入的partial function application部分函數應用particular ordering部分有序pass by adress按址傳遞傳址pass by reference按引用傳遞傳引用pass by value按值傳遞傳值path路徑pattern模式pattern match模式匹配perform執行performance性能performance-criticalpersistence持久性phrenology相面physical物理的pipe管道pixel像素placeholder占位符planning計畫platform平臺pointer指針pointer arithmetic指針運算poll輪詢polymorphic多態polymorphism多態polynomial多項式的pool池port端口portable可移植性portal門戶positional parameters位置參數postfix后序precedence優先級precedence list優先級列表preceding前述的predicate判斷式謂詞preemptive multitasking搶占式多任務premature design過早設計preprocessor預處理器prescribe規定prime素數primitive原語primitive recursive主遞歸primitive type原生類型principal type主要類型print打印printed representation打印表示法printer打印機priority優先級procedure過程procedurual過程化的procedurual knowledge過程式知識process進程process priority進程優先級productivity生產力profile評測profiler評測器性能分析器programmer程序員programming編程programming language編程語言project項目prompt提示符proper list正規列表property屬性property list屬性列表protocol協議prototype原型pseudo code偽碼pseudo instruction偽指令purely functional language純函數式語言pushdown stack下推棧
Q
英文譯法 1譯法 2譯法 3qualified修飾的帶前綴的qualifier修飾符quality質量quality assurance, QA質量保證query查詢query language查詢語言queue隊列quote引用quoted form引用形式
R
英文譯法 1譯法 2譯法 3race condition條件競爭競態條件radian弧度Redundant Array of Independent Disks, RAID冗余獨立磁盤陣列raise引起random number隨機數range范圍區間rank(矩陣)秩排名rapid prototyping快速原型開發rational database關系數據庫raw未經處理的read讀取read-evaluate-print loop, REPL讀取-求值-打印循環read-macro讀取宏record記錄recursion遞歸recursive遞歸的recursive case遞歸情形refactor重構refer參考reference引用參考referential transparency引用透明refine精化reflection反射映像register寄存器registry creep注冊表蠕變regular expression正則表達式represent表現request請求resolution解析度resolve解析rest parameter剩余參數return返回回車return value返回值reuse of software代碼重用right associative右結合Reduced Instruction Set Computer, RISC精簡指令系統計算機robust健壯robustness健壯性魯棒性routine例程routing路由row-major order列主序remote procedure call, RPC遠程過程調用run-length encoding游程編碼run-time typing運行期類型runtime運行期rvalue右值
S
英文譯法 1譯法 2譯法 3S-expressionS-表達式save儲存Secure Sockets Layer, SSL安全套接字層scaffold腳手架鷹架scalar type標量scan掃描schedule調度scheduler調度程序scope作用域SCREAMING_SNAKE_CASE尖叫式蛇底大寫screen屏幕scripting language腳本語言search查找搜尋segment of instructions指令片段semantics語義semaphore信號量semicolon分號sequence序列sequential循序的順序的sequential collection literalsserial串行serialization序列化series串行級數server服務器shadowing隱蔽了sharp犀利的sharp-quote升引號shortest path最短路徑SICP《計算機程序的構造與解釋》side effect副作用signature簽名simple vector簡單向量simulate模擬Single Point of Truth, SPOT真理的單點性single-segment單段的sketch草圖初步框架slash斜線slot槽smart pointer智能指針snake_case蛇底式小寫snapshot屏幕截圖socket套接字software軟件solution方案source code源代碼space leak內存泄漏spaghetti面條式代碼意面式代碼spaghetti stack意面式棧面條式棧spam垃圾郵件spec規格special form特殊形式special variable特殊變量specialization特化specialize特化specialized array特化數組specification規格說明規范splitter切分窗口sprite精靈圖square平方square root平方根squash碰撞stack棧stack frame棧幀stakeholderstandard library標準函式庫state machine狀態機statement陳述語句static type靜態類型static type system靜態類型系統status狀態store保存stream流strict嚴格strict evaluation嚴格求值string字串字符串string template字串模版strong type強類型structural recursion結構遞歸structured values結構型值subroutine子程序subset子集substitution代換substitution model代換模型subtype子類型superclass基類superfluous多余的supertype超集support支持suspend掛起swapping values交換變量的值symbol符號symbolic computation符號計算syntax語法system administrator系統管理員system administrator disease系統管理員綜合癥System Network Architecture, SNA系統網絡體系
T
英文譯法 1譯法 2譯法 3(database)table數據表table表格tag標簽標記tail-recursion尾遞歸tail-recursive尾遞歸的TAOCP《計算機程序設計藝術》target目標taxable operators需節制使用的操作符taxonomy分類法template模版temporary object臨時對象testing測試text文本text file文本文件thread線程thread safe線程安全three-valued logic三值邏輯throw拋出丟擲引發throwaway program一次性程序timestamp時間戳token詞法記號語義單位語元top-down design自頂向下的設計top-level頂層trace追蹤trailing space行尾空白transaction事務transition network轉移網絡transparent透明的traverse遍歷tree樹tree recursion樹形遞歸trigger觸發器tuple元組Turing machine圖靈機Turing complete圖靈完備typable類型合法type類型type constructor類構造器type declaration類型聲明type hierarchy類型層級type inference類型推導type name類型名type safe類型安全type signature類型簽名type synonym類型別名type variable類型變量typing類型指派輸入
U
英文譯法 1譯法 2譯法 3user interface, UI用戶界面unary一元的underflow下溢unification合一統一union并集universally quantify全局量化unqualfied未修飾的unwindinguptime運行時間Uniform Resource Locator, URL統一資源定位符user用戶utilities實用函數
V
英文譯法 1譯法 2譯法 3validate驗證validator驗證器value constructor值構造器vaporware朦朧件variable變量variable capture變量捕捉variadic input可變輸入variant變種venture capitalist, VC風險投資商vector向量viable function可行函數video視頻view視圖virtual function虛函數virtual machine虛擬機virtual memory虛內存volatile揮發vowel元音
W
英文譯法 1譯法 2譯法 3warning message警告信息web server網絡服務器weight權值權重well type類型正確wildcard通配符window窗口word單詞字wrapper包裝器包裝What You See Is What You Get, WYSIWYG所見即所得What You See Is What You Want, WYSIWYW所見即所想
X
Y
英文譯法 1譯法 2譯法 3Y combinatorY組合子
Z
英文譯法 1譯法 2譯法 3Z-expressionZ-表達式zero-indexed零索引的
專業名詞
英文譯法 1譯法 2譯法 3The Paradox of Choice選擇謬論
大家在瀏覽網頁的時候,是否思考過這樣一個問題:怎樣才能制作出一個網頁呢?制作出一個網頁是很簡單的,只要知道什么是HTML并掌握HTML的基礎知識就可以制作出一個簡單的網頁,今天我就為講解HTML的入門及結構組成。
一、什么是HTML?
1、在我們開始學習HTML之前我們需要知道什么是HTML?
HTML的全稱為Hyper Text Markup Language,中文名稱為超文本標記語言,閱覽方式為網頁瀏覽器,同時HTML也被稱為網頁。
2、一個簡單的HTML文檔
二、HTML編輯器
我們在可以使用TXT文本文檔或者專業的HTML編輯器來編輯HTML。
1、記事本
① 創建一個TXT文本
② 輸入HTML代碼
③ 點擊文件—另存為—輸入名稱+“html”的后綴名即可得到你的第一個HTML文件。
④ 然后雙擊這個文件運行。
運行結果
2、專業編輯器
① Sublime Text
② HBuilder
③ Adobe Dreamweaver
④ CoffeeCup HTML Editor
這其中我比較推薦HBuilder這款編輯器,界面簡單,編輯起來很快。
三、HTML的組成部分
在上面的案例中我們可以看到HTML是由頭部(head)和身體(body)所組成的。
1、頭部(head)
通常包含標題(title),也就是一個網頁的名稱
網頁標題
2、身體(body)
body的部分是整個網頁的重要內容部分,讓人一眼就瀏覽到這個網頁的內容,可以插入文本、圖片、多媒體等內容。
四、HTML元素
l HTML元素是指以開始標簽起始,以結束標簽終止的元素:元素內容即為開始標簽與結束標簽之間的內容。
l <head></head>、<body></body>、<p></p>、<h1></h2>等這些都是HTML元素,在上面的案例中就有六個元素。
l 也有部分元素只有開始標簽,例如<br>,以開始標簽的結束而結束。
五、HTML的屬性
l 一般來說HTML的屬性就是HTML元素的屬性,屬性可以在元素中添加附加信息。
l 屬性總是以名稱/值對的形式出現,比如:name=”value”。
l 屬性一般描述于開始標簽。
l style中會有更多的屬性。
六、HTML格式化
HTML可定義很多供格式化輸出的元素,比如粗體字和斜體字。
HTML文本格式化標簽
標簽 | 描述 |
<b> | 定義粗體文本 |
<em> | 定義著重文字 |
<i> | 定義斜體字 |
<small> | 定義小號字 |
<strong> | 定義加重語氣 |
<sub> | 定義下標字 |
<sup> | 定義上標字 |
<ins> | 定義插入字 |
<del> | 定義刪除字 |
運行結果示意圖
HTML“計算機輸出”標簽
標簽 | 描述 |
<code> | 定義計算機代碼 |
<kdd> | 定義鍵盤碼 |
<samp> | 定義計算機代碼樣本 |
<var> | 定義變量 |
<pre> | 定義預格式文本 |
HTML引文、引用及標簽定義
標簽 | 描述 |
<abbr> | 定義縮寫 |
<address> | 定義地址 |
<bdo> | 定義文字方向 |
<blockquote> | 定義長的引用 |
<q> | 定義短的引用語 |
<cite> | 定義引用、引證 |
<dfn> | 定義一個定義項目 |
七、HTML超鏈接
超鏈接可以是圖片、文字、多媒體也可以是一個網址
示例:
結果:
點擊帶有下劃線的兩個字就可以進入某度的網站
八、HTML CSS
1、CSS是一種層疊樣式表,可以修飾html元素的樣式并可以精確地進行排版
2、CSS有三種方式:
l 內部樣式,在HTML元素中使用“style”屬性
l 內部樣式表,在頭部<head>區域使用<style>元素來包含CSS
l 外部引用,引用帶有后綴css的文件,示例:
html文檔
css文檔
如果你看到了這里,就說明你已經打開了制作網頁的大門啦~
大家好,我是小雨!
今天我們要介紹的知識點是 Django 中的 Templates,它也是 MVT 結構中的 T,Templates 可以叫它模板。Templates 究竟用來做什么,通過這一節的介紹,希望大家有一個基本的了解。
在上一節當中,我們在瀏覽器中看到的是 HTTPResponse 返回的字符串,而這一節我們講的 templates 最直接的功能是把前端的 html 頁面顯示出來,這樣用戶就能看到清晰美觀的界面。那么 Django 是如何加載 html 網頁的呢?下面介紹兩種方式:
我們提前準備好一個 html 頁面index.html,然后為了方便 views 中能成功訪問到這個index.html,需要在settings.py中將templates文件夾的路徑聲明清楚。在 TEMPLATES 列表字典的DIRS屬性下添加一下內容:
os.path.join(BASE_DIR, 'templates') # 如果缺少os模塊記得導入
render_to_string 的使用方法: 先使用from django.templates.loader import render_to_string導入模塊,然后給 render_to_string 傳入 html 文件后,最后將其以 HttpResponse 的方式返回出去即可。
from django.http import HttpResponse
from django.templates.loader import render_to_string
def index(request):
html = render_to_string("index.html")
return HttpResponse(html)
這里我們需要從django.shortcut導入 render 模塊,一般情況下 Django 會默認導入這個模塊,views 中如果沒有的話就手動導入一下:
from django.shortcut import render
接著 render 中傳入 html 文件,記得添加 request 參數,然后使用 return 返回 render 即可:
def index(request):
return render(request,'index.html')
最后記得在 urls.py 中將路徑對應的 views.index 添加進來就可以了。
我們要知道 templates 作為 MVT 架構中的 T,不僅僅是加載前端中的頁面,它還包含一種模板語言,能夠在 htmlL 語言中實現邏輯控制(條件選擇、循環),相對于 JavaScript 會更加的靈活。
常用的python模板語言引擎
接下來,我們就以一個簡單的例子來介紹 Django 模板語言的使用。
我們有一個存儲學生信息的Students.csv文件,包含了學號、姓名、性別、手機號碼、出生日期、電子郵箱等信息。
319001,趙一,男,1998/12/27,18706012232,532211428@qq.com,北京市海淀區頤和園路5號,342622199801144314,2019/9/1,計算機,趙一,13655512212
319002,錢二,女,1995/10/25,13459732456,572501101@qq.com,北京市海淀區雙清路30號,342622199709066819,2019/9/1,物聯網,王二,13856909992
這里數據其實有很多條,為了減少篇幅我們就羅列出兩條。
現在需要將這些數據加載到前端的 html 頁面中,通過 tabel 標簽顯示出來。
(1)準備
首先用 pycharm 新建一個 Django 項目,為了方便使用直接把 application 和 templates 文件夾都建好。
pycharm新建Django項目
在 templates 中新建一個 index.html 文件,寫好用于顯示學生信息的標題以及表格框架。
<div id="title">學生信息列表</div>
<div id="table_main">
<table border="1">
<thead>
<tr>
<th>學號</th>
<th>姓名</th>
<th>性別</th>
<th>出生日期</th>
<th>手機號碼</th>
<th>電子郵箱</th>
<th>家庭地址</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
(2)views 設置
為了讀取 csv 文件,寫一個read_form_file方法,將文件中的信息存儲在 Students 列表中。
def read_from_file(path:str):
students = []
try:
with open(path, mode="r", encoding="UTF-8") as fd:
# 讀取當前行
current_line = fd.readline()
# 判斷當前行是否為空
while current_line:
temp_line = current_line.split(",")
students.append(temp_line)
current_line = fd.readline()
return students
except Exception as e:
raise e
接下來,我們定義一個 student 方法,用來顯示 html 內容給前端,這里面我們還是使用 render 將獲取的數據傳遞給前端頁面index.html。
注意使用 context 參數,它的值我們設置為一個字典,鍵設置為 DTL 模板語言中要引用的變量,值設置為本地獲取的數據
def student(request):
students = read_from_file(r"D:\Python\Project\Demo\Student.csv")
return render(request, "index.html", context={"all_student":students})
(3)DTL 模板語言
在index.html中,表格內容的填充,我們通過 DTL 模板語言來完成。在 HTML 代碼中要插入 DTL 模板語言,需要使用{%語句%},for 語句還要使用{% endfor %}來結尾,在語句內部調用變量列表中的元素也是直接使用變量名.數字并且需要包裹兩層大括號。這里我們以循環輸出 student 列表中的各個元素為例,其 DTL 模板語言的寫法如下:
{% for student in all_student %}
<tr>
<td>{{ student.0 }}</td>
<td>{{ student.1 }}</td>
<td>{{ student.2 }}</td>
<td>{{ student.3 }}</td>
<td>{{ student.4 }}</td>
<td>{{ student.5 }}</td>
<td>{{ student.6 }}</td>
</tr>
{% endfor %}
(4)urls 配置
接下來配置路由,添加 student 路徑
path('student/',app01_views.student),
(5)運行效果
一切準備就緒后,執行python manage.py runserver,在瀏覽器中打開127.0.0.1:8000/student效果如下:
數據被成功加載
不能與服務器做動態交互的文件叫靜態文件; 對于 HTML 中的圖片、視頻、css、js 這些都屬于靜態文件。Django 對于這些文件有一套明確的管理機制。 如果我們只遵循 HTML 的規范去加載靜態文件,在 Django 中是顯示不出來的。
Django無法加載靜態文件
為了解決這個問題,我們首先要確保配置文件 settings.py 中 INSTALLED_APP 下靜態文件模塊有被加載到:
'django.contrib.staticfiles', # 必須確保安裝靜態模塊
然后使用STATICFILES_DIRS這個全局變量來存儲靜態文件的路徑。
STATICFILES_DIRS = [
os.path.join(BASE.DIR, 'static'),
]
然后在 html 中開始加上模板語言,如果讓 html 引用靜態文件,那就需要先導入靜態模塊。在 html 語句第一行寫上
{% load static %}
對于要引用靜態圖片的標簽路徑使用{% static "路徑文件名 "%},如:
<img src="{% static "logo.png"%}">
這樣 Django 就能顯示靜態文件圖片了。
靜態圖片被正確顯示了
一般情況下,static 文件夾我們會放在app目錄下。這時候訪問靜態文件,則需要在STATICFILES_DIRS的 os.path.join 參數中再多添加一個子文件夾名,可以寫成:
STATICFILES_DIRS = [
os.path.join(BASE.DIR, 'app01','static'),
]
在 Django 配置的文件 settings.py 中有一個全局變量STATIC_URL,它定義了一個給外界用戶直接訪問靜態文件的路徑。 上面我們添加在標題中的 logo.png,我們可以在瀏覽器中直接通過127.0.0.1:8000/student/static/logo.png來訪問。
直接訪問靜態文件
為了更好地鞏固 Django 加載靜態文件的知識,我們做一個小案例。學生信息管理系統的登錄頁面。有顯示圖片 logo,有用戶名和密碼的輸入框,并且有提交按鈕。當點擊按鈕時,會彈出提示表示登錄成功。
這里簡單說明一下:
界面采用了 Bootstrap 框架,登錄按鈕的功能函數用了 jQuery 腳本。關于前端的知識我們這里就不做過多介紹了。
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
<!--加載外部的CSS文件-->
<link type="text/css" rel="stylesheet" href="{% static "css/bootstrap.min.css" %}">
<link type="text/css" rel="stylesheet" href="{% static "css/basic.css" %}">
<!--加載外部的js文件-->
<script src="{% static "js/jquery.min.js" %}"></script>
<script src="{% static "js/login.js" %}"></script>
</head>
<body>
<div class="container">
<div id = "login">
<form class="form">
<h2><img src="{% static "imags/mainlogo.png" %}"></h2>
<h2>學生信息管理系統登錄</h2>
<hr>
<hr>
<div class="form-group">
<label for="user">用戶名:</label>
<input type="text" class="form-control" placeholder="請輸入用戶名" id="user">
</div>
<div class="form-group">
<label for="password">密碼</label>
<input type="password" class="form-control" placeholder="請輸入密碼" id="password">
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox">請記住我
</label>
</div>
</div>
<button id="submit01" class="form-control">提交</button>
</form>
</div>
</div>
</body>
</html>
將以上 html 文件保存在 templates 中,在 views 中配置好方法,urls 中配置好路由,最后在 settings 中配置好靜態文件路徑,最后啟動 Django 服務器,頁面顯示效果如下:
登錄頁面案例演示
本節,我們介紹了 Django 中 templates 的知識,Django 加載 HTML 方法、模板的語言的使用,以及加載靜態文件的方法。希望大家多多練習,才能鞏固所學的知識。下一節我們將繼續介紹 Django 中 URL 跳轉的知識!
感謝大家的閱讀!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。