<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 功能強大 支持多語言、二開方便! 廣告
                * 視圖必須返回一個`HttpResponse`對象(或其子類對象),不能像Flask一樣直接返回字符串 * 可以將要返回的字符串數據傳給`HttpResponse`對象再返回 * `HttpRequest`對象由Django創建,`HttpResponse`對象由開發人員創建 * `HttpResponse`的常用子類 * `HttpResponseRedirect`重定向 * `JsonResponse`返回json數據 ## 1. HttpResponse 1. 可以使用`django.http.HttpResponse`來構造響應對象 ~~~ HttpResponse(content=響應體, content_type=響應體數據MIME類型, status=狀態碼) ~~~ `MIME(Multipurpose Internet Mail Extensions)`多用途互聯網郵件擴展類型: * `text/html`html * `text/plain`普通文本 * `application/json`json 2. 響應頭設置: 可以直接將`HttpResponse`對象當做字典進行響應頭鍵值對的設置 ~~~ response = HttpResponse('響應內容') response['Itcast'] = 'Python' # 自定義響應頭Itcast, 值為Python ~~~ ## 2. JsonResponse對象 1. 幫助我們將數據轉換為json字符串,再返回給客戶端 2. 會設置響應頭`Content-Type`為`application/json` ~~~ from django.http import JsonResponse def resp(request): return JsonResponse({'city': 'beijing', 'subject': 'python'}) ~~~ 3. 當包含的內容中包含中文時,會返回該中文對應的編碼,例如: ~~~ def resp(request): # 最終看到的效果是: {"name": "\u5f20\u4e09"} response = JsonResponse({"name":"張三"}) return response ~~~ 解決:`JsonResponse(data, json_dumps_params={'ensure_ascii':False})` 4. JsonResponse可以接收非字典數據,需要指定`safe=False` ## 3\. redirect重定向 * 重定向: 不返回具體顯示內容給客戶端,讓客戶端重新請求返回的地址,獲取內容 ~~~ from django.shortcuts import redirect def resp(request): # 重定向到首頁 # return HttpResponseRedirect('/index') return redirect('/index') ~~~ # reverse函數 reverse函數: 動態生成URL地址,解決url硬編碼維護麻煩的問題。 使用方式: 1. 配置url ~~~ # 項目的urls.py urlpatterns = [ # 示例: 應用名可指定為 users url(r'^', include('users.urls', namespace='應用名')), ] # 應用的urls.py urlpatterns = [ # 示例: url配置項名 可指定為 index url(r'^index$', views.index, name='url配置項名'), ] ~~~ 2. 動態生成url地址 ~~~ url = reverse('命名空間:url配置項名稱') # 或者,可以沒有命名空間: url = reverse('url配置項名稱') # 示例1: 生成的url的值為 /index url = reverse('users:index') # 示例2: 生成的url的值為 /index url = reverse('index') ~~~
                  <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>

                              哎呀哎呀视频在线观看