<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 功能強大 支持多語言、二開方便! 廣告
                # URLconf ## URLconf * 在settings.py文件中通過ROOT\_URLCONF指定根級url的配置 * urlpatterns是一個url\(\)實例的列表 * 一個url\(\)對象包括: * 正則表達式 * 視圖函數 * 名稱name * 編寫URLconf的注意: * 若要從url中捕獲一個值,需要在它周圍設置一對圓括號 * 不需要添加一個前導的反斜杠,如應該寫作"test/",而不應該寫作"/test/" * 每個正則表達式前面的r表示字符串不轉義 * 請求的url被看做是一個普通的python字符串,進行匹配時不包括get或post請求的參數及域名 ```text localhost:8000/1/?a=1&b=2,只匹配"/1/"的部分 ``` * 正則表達式非命名組,通過位置參數傳遞給視圖 ```text url(r'^([0-9]+)/$', views.detail, name='detail'), ``` * 正則表達式命名組,通過關鍵字參數傳遞給視圖,本例中關鍵字參數為id ```text url(r'^detail/(?P<id>[0-9]+)/$',views.detail) ``` * 參數匹配規則:優先使用命名參數,如果沒有命名參數,則使用位置參數 * 每個捕獲的參數都作為一個普通的python字符串傳遞給視圖 * 性能:urlpatterns中的每個正則表達式在第一次訪問它們時被編譯,使得系統性能相當快 ```text url.py """ url(r'^detail/(?P<id>[0-9]+)/$', views.detail), """ views.py """ def detail(request,id): return render(request,'myapp/detail.html',{"id":id}) """ ``` ## 包含其他的urls * 在應用中創建urls.py文件,定義本應用中的urlconf,再在項目的settings中使用include\(\) ```text from django.conf.urls import include,url urlpatterns = [ url(r'^',include(('myapp.urls',"myapp"),namespace="myapp")), ] ``` * 匹配過程:先與主URLconf匹配,成功后再用剩余的部分與應用中的URLconf匹配 ```text 請求http://www.itcast.cn/booktest/1/ ``` ```text 在sesstings.py中的配置: url(r'^booktest/', include('booktest.urls', namespace='booktest')), 在booktest應用urls.py中的配置 url(r'^([0-9]+)/$', views.detail, name='detail'), 匹配部分是:/booktest/1/ 匹配過程:在settings.py中與“booktest/”成功,再用“1/”與booktest應用的urls匹配 ``` * 使用include可以去除urlconf的冗余 * 參數:視圖會收到來自父URLconf、當前URLconf捕獲的所有參數 * 在include中通過namespace定義命名空間,用于反解析 ## url的反響解析 * 如果在視圖、模板中使用硬編碼的鏈接,在urlconf發生改變時,維護是一件非常麻煩的事情 * 解決:在做鏈接時,通過指向urlconf的名稱,動態生成鏈接地址 * 視圖:使用from django.urls import reverse函數 * 模板:使用url模板標簽
                  <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>

                              哎呀哎呀视频在线观看