Django 學(xué)習(xí)筆記 - 編寫第一個Django頁面

編寫第一個Django頁面

編寫流程

  1. 數(shù)據(jù)模型(finished)
  2. URL配置
  3. 控制器
  4. 頁面

URL配置

總url.py的代碼

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', "block.views.block_list"),
]
  • r 防止后面的字符串被轉(zhuǎn)義

views.py (控制器)編寫

from django.shortcuts import render_to_response

from models import Block

def block_list(request):
    blocks = Block.objects.all().order_by("-id")
    return render_to_response("block_list.html", {"blocks": blocks})
  • render_to_response 渲染及返回模板
  • .objects 對數(shù)據(jù)庫操作
  • -id 負(fù)號表示倒序
  • blocks 的類型是一個django定義的數(shù)據(jù)類型??set,查詢的結(jié)果集
  • render_to_response 字典是給模板渲染的變量,可以多個key

頁面模板

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Python部落論壇</title>
</head>
<body>
    <h1>Python部落論壇</h1>
    {% for block in blocks %}
    <div class="blockitem">
        {{ block.name }}
        {{ block.decs }}
        {{ block.manager.username }}
    </div>
    {% endfor %}
</body>
</html>
  • for 對每一個block輸出一遍開閉標(biāo)簽部分的html代碼
  • {{ }} django 語法,把大括號里面的值輸出

創(chuàng)建模板html文件

mkdir block/templates
touch block/templates/block_list.html

用bootstrap美化頁面

<link rel="stylesheet" >

grid網(wǎng)格來劃分布局

<div class="container">
    <div class="row">
        <div class="col-sm-10 col-md-6">
        A
        </div>
        <div class="col-sm-10 col-md-6">
        B
        </div>
    </div>
</div>

表格

<table class="table">
    <tr>
        <td>A</td>
        <td>B</td>
    </tr>
    <tr>
        <td>C</td>
        <td>D</td>
    </tr>
</table>
  • table class 指定表格的樣式table table-stipe table-bordered table-hover等

按鈕

<button type="button" class="btn btn-success"></button>
  • bootstrap 定義class,class里面兩個屬性 第一個基本 第二個形式

well 井

<div class="well">...</div>

文檔閱讀

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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