<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Response對象 ## HttpResponse對象 * 在django.http模塊中定義了HttpResponse對象的API * HttpRequest對象由Django自動創建,HttpResponse對象由程序員創建 * 不調用模板,直接返回數據 ```text from django.http import HttpResponse def index(request): return HttpResponse("你好") ``` * 調用模板 ```text from djangp.http import HttpResponse from django.template import RequestContext,loader def index(request): templates = loader.get_template('myapp/index.html') context ={'h':'hello'} return HttpResponse(templates.render(context,request)) ``` ## 屬性 * content:表示返回的內容,字符串類型 * charset:表示response采用的編碼字符集,字符串類型 * status\_code:響應的HTTP響應狀態碼 * content-type:指定輸出的MIME類型 ## 方法 * init:使用也內容實例化HttpResponse對象 * write\(content\):以文件的方式寫 * flush\(\):以文件的方式輸出緩存區 * set\_cookie\(key,value=",max\_age=None,expires=None\):設置Cookie * key、value都是字符串類型 * max\_age是一個整數目標是在指定秒數后過期 * expires是一個datetime或timedelta對象,會話將在這個指定的日期/時間過期,注意datetime和timedelta值只有在使用PickleSerializer時才可序列化 * max\_age與expires二選一 * 如果不指定過期時間,則兩個星期后過期 ```text from django.http import HttpReponse from datetime import * ``` * 創建cookie ```text from django.http import HttpResponse def index(request): response = HttpResponse() cookie = request.COOKIES response.set_cookie('name', 'angle') #response.set_cookie('h1', '你好', None, datetime(2016, 10, 31)) return response ``` * 在屏幕上輸出cookie值 ```text from django.http import HttpResponse def index(request): response = HttpResponse() cookie = request.COOKIES response.write('<h1>' + cookie['name'] + '</h1>') return response ``` * delete\_cookie\(key\):刪除指定的key的Cookie,如果key不存在則什么也不發生 ## 子類HttpResponseRedirect * 重定向,服務器端跳轉 * 構造函數的第一個參數用來指定重定向的地址 ```text from django.http import HttpResponse,HttpResponseRedirect def index(request): return HttpResponseRedirect('js/') def index2(request,id): return HttpResponse(id) ``` 請求地址:127.0.0.1:8000/index 請求結果地址:127.0.0.1:8000/index/js/ * 反向解析 ```text from django.urls import reverse def index(request): print(reverse('myapp:index')) # /sunck/index/ return render(request,'myapp/index.html') ``` * index.html ```text <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>學生信息</title> </head> <body> <h1>學生列表</h1> <a href="/good/123/">鏈接1</a> # {# 硬鏈接 <a href="{% url 'myapp:good' 1%}">鏈接2</a> # {# 反向解析 </body> </html> ``` * 請求地址:[http://localhost:8000/sunck/index/](http://localhost:8000/sunck/index/) * 第一個鏈接:[http://localhost:8000/good/123/](http://localhost:8000/good/123/) * 第二個鏈接:[http://localhost:8000/sunck/good/1](http://localhost:8000/sunck/good/1) * 注意: * 在urls.py中添加 ```text app_name = 'myapp' ``` * 在test2/urls.py中添加 ```text url(r'^sunck/',include('myapp.urls',namespace='myapp')), ``` ## 子類JsonResponse * 返回json數據,一般用于異步請求 * _init_\(data\) * 幫助用戶創建json編碼的響應 * 參數data是字典對象 * JsonResponse的默認Content-Type為application/json ```text from django.http import JsonResponse def jsonTest(request): return JsonResponse({'list':'abc'}) 配置路由: url(r'^js/$',views.jsonTest,name="js"), ``` ## 簡寫函數 ### render * render\(request,template\_name\[,context\]\) * 結合一個給定的模板和一個給定的上下文字典,并返回一個渲染后的HttpResponse對象 * request:該request用于生產response * template\_name:要使用的模板的完整名稱 * context:添加到模板上下文的一個字典,視圖將在渲染模板之間調用它 ```text from django.shortcuts import render def index(request): retuen render(request,'myapp/index.html',{'name':'angle'}) ``` ### **重定向** * redirect\(url\) * 為傳遞進來的參數返回HttpReponseRedirect * url推薦使用反向解析 ```text def showindex(request): print(reverse("myapp:index")) # return redirect(reverse('myapp:index')) return redirect('/sunck/index') return redirect(reverse('myapp:index')) //重定向訪問index.html ``` ### 得到對象或返回404 * get\_object\_or-404\(klass,\*args,\*\*kwargs\) * 通過模型管理器或查詢集調用get\(\)方法,如果沒有找到對象,不引發模型的DoesNotExist異常,而是引發Http404異常 * klass:獲取對象的模型類、Manager對象或QuerySet對象 * \*\*kwargs:查詢的參數,格式應該可以被get\(\)和filter\(\)接受 * 如果找到多個對象將引發MultipleObjectReturned ```text from django.shortcuts import * def detail(request,id): try: g = get_object_or_404(Grades,gname="python01") except Grades.MultipleObjectsReturned: return HttpResponse('error') return render(request,'myapp/detail.html',{'grade':g,"id":id}) # 將settings.py中的DEBUG改為False # 將請求地址輸入2和100查看效果 ``` ## 得列表或返回404 * get\_list\_or\_404\(klass,args,\*kwargs\) * klass:獲取列表的一個Model,Manager或QuerySet實例 * \*\*kwargs:查詢的參數,格式應該可以被get\(\)和filter\(\)接受 ```text from django.shortcuts import * def index(request): list = get_list_or_404(Grades,pk__lt==6) return render(request,'myapp/detail.html',{'grade':g) ``` 將settings.py中的DEBUG改為False
                  <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>

                              哎呀哎呀视频在线观看