<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### 模板 * 模板是html頁面,可以根據視圖中傳遞的數據填充值 * 創建模板的目錄如下圖: ![](https://box.kancloud.cn/9a30f639b3f6db4e0a70022fe472dbf6_219x94.png) * 修改settings.py文件,設置TEMPLATES的DIRS值 ~~~ 'DIRS': [os.path.join(BASE_DIR, 'templates')], ~~~ * 在模板中訪問視圖傳遞的數據 ~~~ {{輸出值,可以是變量,也可以是對象.屬性}} {%執行代碼段%} ~~~ #### 定義index.html模板 ~~~ <!DOCTYPE html> <html> <head> <title>首頁</title> </head> <body> <h1>圖書列表</h1> <ul> {%for book in booklist%} <li> <a href="{{book.id}}"> {{book.btitle}} </a> </li> {%endfor%} </ul> </body> </html> ~~~ 定義detail.html模板 * 在模板中訪問對象成員時,都以屬性的方式訪問,即方法也不能加括號 ~~~ <!DOCTYPE html> <html> <head> <title>詳細頁</title> </head> <body> <h1>{{book.btitle}}</h1> <ul> {%for hero in book.heroinfo_set.all%} <li>{{hero.hname}}---{{hero.hcontent}}</li> {%endfor%} </ul> </body> </html> ~~~ * * * * * ### 使用模板 * 編輯views.py文件,在方法中調用模板 ~~~ from django.http import HttpResponse from django.template import RequestContext, loader from models import BookInfo def index(request): booklist = BookInfo.objects.all() template = loader.get_template('booktest/index.html') context = RequestContext(request, {'booklist': booklist}) return HttpResponse(template.render(context)) def detail(reqeust, id): book = BookInfo.objects.get(pk=id) template = loader.get_template('booktest/detail.html') context = RequestContext(reqeust, {'book': book}) return HttpResponse(template.render(context)) ~~~ * * * * * ### 去除模板的硬編碼 * 在index.html模板中,超鏈接是硬編碼的,此時的請求地址為“127.0.0.1/1/” ~~~ <a href="{{book.id}}"> ~~~ * 看如下情況:將urlconf中詳細頁改為如下,鏈接就找不到了 ~~~ url(r'^book/([0-9]+)/$', views.detail), ~~~ * 此時的請求地址應該為“127.0.0.1/book/1/” * 問題總結:如果在模板中地址硬編碼,將來urlconf修改后,地址將失效 * 解決:使用命名的url設置超鏈接 * 修改test1/urls.py文件,在include中設置namespace ~~~ url(r'^admin/', include(admin.site.urls, namespace='booktest')), ~~~ 修改booktest/urls.py文件,設置name ~~~ url(r'^book/([0-9]+)/$', views.detail, name="detail"), ~~~ 修改index.html模板中的鏈接 ~~~ <a href="{%url 'booktest:detail' book.id%}"> ~~~ * * * * * ### Render簡寫 Django提供了函數Render()簡化視圖調用模板、構造上下文 ~~~ from django.shortcuts import render from models import BookInfo def index(reqeust): booklist = BookInfo.objects.all() return render(reqeust, 'booktest/index.html', {'booklist': booklist}) def detail(reqeust, id): book = BookInfo.objects.get(pk=id) return render(reqeust, 'booktest/detail.html', {'book': book}) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看