Request鍜孯esponse

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Request鍜孯esponse相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/one' title='one'>one   func   鍔犺浇   project   鎻愪氦   鏀诲嚮   this   code   灏嗕笉   

Request瀵硅薄鍜孯esponse瀵硅薄

 

request瀵硅薄

褰撲竴涓〉闈㈣璇锋眰鏃讹紝Django灏变細鍒涘缓涓€涓寘鍚湰娆¤姹傚師淇℃伅鐨凥ttpRequest瀵硅薄銆?br />Django浼氬皢杩欎釜瀵硅薄鑷姩浼犻€掔粰鍝嶅簲鐨勮鍥惧嚱鏁帮紝涓€鑸鍥惧嚱鏁扮害瀹氫織鎴愬湴浣跨敤 request 鍙傛暟鎵挎帴杩欎釜瀵硅薄銆?/p>

瀹樻柟鏂囨。

 

璇锋眰鐩稿叧鐨勫父鐢ㄥ€?/h3>
  • path_info     杩斿洖鐢ㄦ埛璁块棶url锛屼笉鍖呮嫭鍩熷悕
  • method        璇锋眰涓娇鐢ㄧ殑HTTP鏂规硶鐨勫瓧绗︿覆琛ㄧず锛屽叏澶у啓琛ㄧず銆?/li>
  • GET              鍖呭惈鎵€鏈塇TTP  GET鍙傛暟鐨勭被瀛楀吀瀵硅薄
  • POST           鍖呭惈鎵€鏈塇TTP POST鍙傛暟鐨勭被瀛楀吀瀵硅薄
  • body            璇锋眰浣擄紝byte绫诲瀷 request.POST鐨勬暟鎹氨鏄粠body閲岄潰鎻愬彇鍒扮殑

灞炴€?/h3>

鎵€鏈夌殑灞炴€у簲璇ヨ璁や负鏄彧璇荤殑锛岄櫎闈炲彟鏈夎鏄庛€?/p>

鎶€鏈浘鐗? id=
灞炴€э細
銆€銆€django灏嗚姹傛姤鏂囦腑鐨勮姹傝銆佸ご閮ㄤ俊鎭€佸唴瀹逛富浣撳皝瑁呮垚 HttpRequest 绫讳腑鐨勫睘鎬с€?
   闄や簡鐗规畩璇存槑鐨勪箣澶栵紝鍏朵粬鍧囦负鍙鐨勩€?


0.HttpRequest.scheme
   琛ㄧず璇锋眰鏂规鐨勫瓧绗︿覆锛堥€氬父涓篽ttp鎴杊ttps锛?

1.HttpRequest.body

銆€銆€涓€涓瓧绗︿覆锛屼唬琛ㄨ姹傛姤鏂囩殑涓讳綋銆傚湪澶勭悊闈?HTTP 褰㈠紡鐨勬姤鏂囨椂闈炲父鏈夌敤锛屼緥濡傦細浜岃繘鍒跺浘鐗囥€乆ML,Json绛夈€?

銆€銆€浣嗘槸锛屽鏋滆澶勭悊琛ㄥ崟鏁版嵁鐨勬椂鍊欙紝鎺ㄨ崘杩樻槸浣跨敤 HttpRequest.POST 銆?

銆€銆€鍙﹀锛屾垜浠繕鍙互鐢?python 鐨勭被鏂囦欢鏂规硶鍘绘搷浣滃畠锛岃鎯呭弬鑰?HttpRequest.read() 銆?

 

2.HttpRequest.path

銆€銆€涓€涓瓧绗︿覆锛岃〃绀鸿姹傜殑璺緞缁勪欢锛堜笉鍚煙鍚嶏級銆?

銆€銆€渚嬪锛?/span>"/music/bands/the_beatles/"



3.HttpRequest.method

銆€銆€涓€涓瓧绗︿覆锛岃〃绀鸿姹備娇鐢ㄧ殑HTTP 鏂规硶銆傚繀椤讳娇鐢ㄥぇ鍐欍€?

銆€銆€渚嬪锛?/span>"GET"銆?span style="color: #800000;">"POST"

 

4.HttpRequest.encoding

銆€銆€涓€涓瓧绗︿覆锛岃〃绀烘彁浜ょ殑鏁版嵁鐨勭紪鐮佹柟寮忥紙濡傛灉涓?None 鍒欒〃绀轰娇鐢?DEFAULT_CHARSET 鐨勮缃紝榛樿涓?鈥?/span>utf-8鈥?/span>锛夈€?
   杩欎釜灞炴€ф槸鍙啓鐨勶紝浣犲彲浠ヤ慨鏀瑰畠鏉ヤ慨鏀硅闂〃鍗曟暟鎹娇鐢ㄧ殑缂栫爜銆?
   鎺ヤ笅鏉ュ灞炴€х殑浠讳綍璁块棶锛堜緥濡備粠 GET 鎴?POST 涓鍙栨暟鎹級灏嗕娇鐢ㄦ柊鐨?encoding 鍊笺€?
   濡傛灉浣犵煡閬撹〃鍗曟暟鎹殑缂栫爜涓嶆槸 DEFAULT_CHARSET 锛屽垯浣跨敤瀹冦€?

 

5.HttpRequest.GET 

銆€銆€涓€涓被浼间簬瀛楀吀鐨勫璞★紝鍖呭惈 HTTP GET 鐨勬墍鏈夊弬鏁般€傝鎯呰鍙傝€?QueryDict 瀵硅薄銆?

 

6.HttpRequest.POST

銆€銆€涓€涓被浼间簬瀛楀吀鐨勫璞★紝濡傛灉璇锋眰涓寘鍚〃鍗曟暟鎹紝鍒欏皢杩欎簺鏁版嵁灏佽鎴?QueryDict 瀵硅薄銆?

銆€銆€POST 璇锋眰鍙互甯︽湁绌虹殑 POST 瀛楀吀 —— 濡傛灉閫氳繃 HTTP POST 鏂规硶鍙戦€佷竴涓〃鍗曪紝浣嗘槸琛ㄥ崟涓病鏈変换浣曠殑鏁版嵁锛孮ueryDict 瀵硅薄渚濈劧浼氳鍒涘缓銆?
   鍥犳锛屼笉搴旇浣跨敤 if request.POST  鏉ユ鏌ヤ娇鐢ㄧ殑鏄惁鏄疨OST 鏂规硶锛涘簲璇ヤ娇鐢?if request.method == "POST" 

銆€銆€鍙﹀锛氬鏋滀娇鐢?POST 涓婁紶鏂囦欢鐨勮瘽锛屾枃浠朵俊鎭皢鍖呭惈鍦?FILES 灞炴€т腑銆?

 7.HttpRequest.COOKIES

銆€銆€涓€涓爣鍑嗙殑Python 瀛楀吀锛屽寘鍚墍鏈夌殑cookie銆傞敭鍜屽€奸兘涓哄瓧绗︿覆銆?

 

8.HttpRequest.FILES

銆€銆€涓€涓被浼间簬瀛楀吀鐨勫璞★紝鍖呭惈鎵€鏈夌殑涓婁紶鏂囦欢淇℃伅銆?
   FILES 涓殑姣忎釜閿负<input type="file" name="" /> 涓殑name锛屽€煎垯涓哄搴旂殑鏁版嵁銆?

銆€銆€娉ㄦ剰锛孎ILES 鍙湁鍦ㄨ姹傜殑鏂规硶涓篜OST 涓旀彁浜ょ殑<form> 甯︽湁enctype="multipart/form-data" 鐨勬儏鍐典笅鎵嶄細
   鍖呭惈鏁版嵁銆傚惁鍒欙紝FILES 灏嗕负涓€涓┖鐨勭被浼间簬瀛楀吀鐨勫璞°€?

 

9.HttpRequest.META

 銆€銆€涓€涓爣鍑嗙殑Python 瀛楀吀锛屽寘鍚墍鏈夌殑HTTP 棣栭儴銆傚叿浣撶殑澶撮儴淇℃伅鍙栧喅浜庡鎴风鍜屾湇鍔″櫒锛屼笅闈㈡槸涓€浜涚ず渚嬶細

    CONTENT_LENGTH —— 璇锋眰鐨勬鏂囩殑闀垮害锛堟槸涓€涓瓧绗︿覆锛夈€?
    CONTENT_TYPE —— 璇锋眰鐨勬鏂囩殑MIME 绫诲瀷銆?
    HTTP_ACCEPT —— 鍝嶅簲鍙帴鏀剁殑Content-Type銆?
    HTTP_ACCEPT_ENCODING —— 鍝嶅簲鍙帴鏀剁殑缂栫爜銆?
    HTTP_ACCEPT_LANGUAGE —— 鍝嶅簲鍙帴鏀剁殑璇█銆?
    HTTP_HOST —— 瀹㈡湇绔彂閫佺殑HTTP Host 澶撮儴銆?
    HTTP_REFERER —— Referring 椤甸潰銆?
    HTTP_USER_AGENT —— 瀹㈡埛绔殑user-agent 瀛楃涓层€?
    QUERY_STRING —— 鍗曚釜瀛楃涓插舰寮忕殑鏌ヨ瀛楃涓诧紙鏈В鏋愯繃鐨勫舰寮忥級銆?
    REMOTE_ADDR —— 瀹㈡埛绔殑IP 鍦板潃銆?
    REMOTE_HOST —— 瀹㈡埛绔殑涓绘満鍚嶃€?
    REMOTE_USER —— 鏈嶅姟鍣ㄨ璇佸悗鐨勭敤鎴枫€?
    REQUEST_METHOD —— 涓€涓瓧绗︿覆锛屼緥濡?/span>"GET" 鎴?span style="color: #800000;">"POST"銆?
    SERVER_NAME —— 鏈嶅姟鍣ㄧ殑涓绘満鍚嶃€?
    SERVER_PORT —— 鏈嶅姟鍣ㄧ殑绔彛锛堟槸涓€涓瓧绗︿覆锛夈€?
 銆€銆€浠庝笂闈㈠彲浠ョ湅鍒帮紝闄?CONTENT_LENGTH 鍜?CONTENT_TYPE 涔嬪锛岃姹備腑鐨勪换浣?HTTP 棣栭儴杞崲涓?META 鐨勯敭鏃讹紝
    閮戒細灏嗘墍鏈夊瓧姣嶅ぇ鍐欏苟灏嗚繛鎺ョ鏇挎崲涓轰笅鍒掔嚎鏈€鍚庡姞涓?HTTP_  鍓嶇紑銆?
    鎵€浠ワ紝涓€涓彨鍋?X-Bender 鐨勫ご閮ㄥ皢杞崲鎴?META 涓殑 HTTP_X_BENDER 閿€?

 
10.HttpRequest.user

銆€銆€涓€涓?AUTH_USER_MODEL 绫诲瀷鐨勫璞★紝琛ㄧず褰撳墠鐧诲綍鐨勭敤鎴枫€?

銆€銆€濡傛灉鐢ㄦ埛褰撳墠娌℃湁鐧诲綍锛寀ser 灏嗚缃负 django.contrib.auth.models.AnonymousUser 鐨勪竴涓疄渚嬨€備綘鍙互閫氳繃 is_authenticated() 鍖哄垎瀹冧滑銆?

    渚嬪锛?

    if request.user.is_authenticated():
        # Do something for logged-in users.
    else:
        # Do something for anonymous users.
     

     銆€銆€user 鍙湁褰揇jango 鍚敤 AuthenticationMiddleware 涓棿浠舵椂鎵嶅彲鐢ㄣ€?

     -------------------------------------------------------------------------------------

    鍖垮悕鐢ㄦ埛
    class models.AnonymousUser

    django.contrib.auth.models.AnonymousUser 绫诲疄鐜颁簡django.contrib.auth.models.User 鎺ュ彛锛屼絾鍏锋湁涓嬮潰鍑犱釜涓嶅悓鐐癸細

    id 姘歌繙涓篘one銆?
    username 姘歌繙涓虹┖瀛楃涓层€?
    get_username() 姘歌繙杩斿洖绌哄瓧绗︿覆銆?
    is_staff 鍜?is_superuser 姘歌繙涓篎alse銆?
    is_active 姘歌繙涓?False銆?
    groups 鍜?user_permissions 姘歌繙涓虹┖銆?
    is_anonymous() 杩斿洖True 鑰屼笉鏄疐alse銆?
    is_authenticated() 杩斿洖False 鑰屼笉鏄疶rue銆?
    set_password()銆乧heck_password()銆乻ave() 鍜宒elete() 寮曞彂 NotImplementedError銆?
    New in Django 1.8:
    鏂板 AnonymousUser.get_username() 浠ユ洿濂藉湴妯℃嫙 django.contrib.auth.models.User銆?

 

11.HttpRequest.session

 銆€銆€涓€涓棦鍙鍙堝彲鍐欑殑绫讳技浜庡瓧鍏哥殑瀵硅薄锛岃〃绀哄綋鍓嶇殑浼氳瘽銆傚彧鏈夊綋Django 鍚敤浼氳瘽鐨勬敮鎸佹椂鎵嶅彲鐢ㄣ€?
    瀹屾暣鐨勭粏鑺傚弬瑙佷細璇濈殑鏂囨。銆?/span>
request灞炴€х浉鍏?/span>

涓婁紶鏂囦欢绀轰緥

鎶€鏈浘鐗? id=
def upload(request):
    """
    淇濆瓨涓婁紶鏂囦欢鍓嶏紝鏁版嵁闇€瑕佸瓨鏀惧湪鏌愪釜浣嶇疆銆傞粯璁ゅ綋涓婁紶鏂囦欢灏忎簬2.5M鏃讹紝django浼氬皢涓婁紶鏂囦欢鐨勫叏閮ㄥ唴瀹硅杩涘唴瀛樸€備粠鍐呭瓨璇诲彇涓€娆★紝鍐欑鐩樹竴娆°€?
    浣嗗綋涓婁紶鏂囦欢寰堝ぇ鏃讹紝django浼氭妸涓婁紶鏂囦欢鍐欏埌涓存椂鏂囦欢涓紝鐒跺悗瀛樻斁鍒扮郴缁熶复鏃舵枃浠跺す涓€?
    :param request: 
    :return: 
    """
    if request.method == "POST":
        # 浠庤姹傜殑FILES涓幏鍙栦笂浼犳枃浠剁殑鏂囦欢鍚嶏紝file涓洪〉闈笂type=files绫诲瀷input鐨刵ame灞炴€у€?/span>
        filename = request.FILES["file"].name
        # 鍦ㄩ」鐩洰褰曚笅鏂板缓涓€涓枃浠?/span>
        with open(filename, "wb") as f:
            # 浠庝笂浼犵殑鏂囦欢瀵硅薄涓竴鐐逛竴鐐硅
            for chunk in request.FILES["file"].chunks():
                # 鍐欏叆鏈湴鏂囦欢
                f.write(chunk)
        return HttpResponse("涓婁紶OK")
涓婁紶浠g爜绀轰緥

鏂规硶

鎶€鏈浘鐗? id=
1.HttpRequest.get_host()

銆€銆€鏍规嵁浠嶩TTP_X_FORWARDED_HOST锛堝鏋滄墦寮€ USE_X_FORWARDED_HOST锛岄粯璁や负False锛夊拰 HTTP_HOST 澶撮儴淇℃伅杩斿洖璇锋眰鐨勫師濮嬩富鏈恒€?
   濡傛灉杩欎袱涓ご閮ㄦ病鏈夋彁渚涚浉搴旂殑鍊硷紝鍒欎娇鐢⊿ERVER_NAME 鍜孲ERVER_PORT锛屽湪PEP 3333 涓湁璇︾粏鎻忚堪銆?

銆€銆€USE_X_FORWARDED_HOST锛氫竴涓竷灏斿€硷紝鐢ㄤ簬鎸囧畾鏄惁浼樺厛浣跨敤 X-Forwarded-Host 棣栭儴锛屼粎鍦ㄤ唬鐞嗚缃簡璇ラ閮ㄧ殑鎯呭喌涓嬶紝鎵嶅彲浠ヨ浣跨敤銆?

銆€銆€渚嬪锛?/span>"127.0.0.1:8000"

銆€銆€娉ㄦ剰锛氬綋涓绘満浣嶄簬澶氫釜浠g悊鍚庨潰鏃讹紝get_host() 鏂规硶灏嗕細澶辫触銆傞櫎闈炰娇鐢ㄤ腑闂翠欢閲嶅啓浠g悊鐨勯閮ㄣ€?

 

2.HttpRequest.get_full_path()

銆€銆€杩斿洖 path锛屽鏋滃彲浠ュ皢鍔犱笂鏌ヨ瀛楃涓层€?

銆€銆€渚嬪锛?/span>"/music/bands/the_beatles/?print=true"

 

3.HttpRequest.get_signed_cookie(key, default=RAISE_ERROR, salt=鈥樷€?/span>, max_age=None)

銆€銆€杩斿洖绛惧悕杩囩殑Cookie 瀵瑰簲鐨勫€硷紝濡傛灉绛惧悕涓嶅啀鍚堟硶鍒欒繑鍥瀌jango.core.signing.BadSignature銆?

銆€銆€濡傛灉鎻愪緵 default 鍙傛暟锛屽皢涓嶄細寮曞彂寮傚父骞惰繑鍥?default 鐨勫€笺€?

銆€銆€鍙€夊弬鏁皊alt 鍙互鐢ㄦ潵瀵瑰畨鍏ㄥ瘑閽ュ己鍔涙敾鍑绘彁渚涢澶栫殑淇濇姢銆俶ax_age 鍙傛暟鐢ㄤ簬妫€鏌ookie 瀵瑰簲鐨勬椂闂存埑浠ョ‘淇滳ookie 鐨勬椂闂翠笉浼氳秴杩噈ax_age 绉掋€?

        澶嶅埗浠g爜
        >>> request.get_signed_cookie(鈥?/span>name鈥?/span>)
        鈥?/span>Tony鈥?/span>
        >>> request.get_signed_cookie(鈥?/span>name鈥?/span>, salt=鈥?/span>name-salt鈥?/span>)
        鈥?/span>Tony鈥?/span> # 鍋囪鍦ㄨ缃甤ookie鐨勬椂鍊欎娇鐢ㄧ殑鏄浉鍚岀殑salt
        >>> request.get_signed_cookie(鈥?/span>non-existing-cookie鈥?/span>)
        ...
        KeyError: 鈥?/span>non-existing-cookie鈥?/span>    # 娌℃湁鐩稿簲鐨勯敭鏃惰Е鍙戝紓甯?/span>
        >>> request.get_signed_cookie(鈥?/span>non-existing-cookie鈥?/span>, False)
        False
        >>> request.get_signed_cookie(鈥?/span>cookie-that-was-tampered-with鈥?/span>)
        ...
        BadSignature: ...    
        >>> request.get_signed_cookie(鈥?/span>name鈥?/span>, max_age=60)
        ...
        SignatureExpired: Signature age 1677.3839159 > 60 seconds
        >>> request.get_signed_cookie(鈥?/span>name鈥?/span>, False, max_age=60)
        False
        澶嶅埗浠g爜
         


4.HttpRequest.is_secure()

銆€銆€濡傛灉璇锋眰鏃舵槸瀹夊叏鐨勶紝鍒欒繑鍥濼rue锛涘嵆璇锋眰閫氭槸杩?HTTPS 鍙戣捣鐨勩€?

 

5.HttpRequest.is_ajax()

銆€銆€濡傛灉璇锋眰鏄€氳繃XMLHttpRequest 鍙戣捣鐨勶紝鍒欒繑鍥濼rue锛屾柟娉曟槸妫€鏌?HTTP_X_REQUESTED_WITH 鐩稿簲鐨勯閮ㄦ槸鍚︽槸瀛楃涓?/span>鈥?/span>XMLHttpRequest鈥?/span>銆?

銆€銆€澶ч儴鍒嗙幇浠g殑 javascript 搴撻兘浼氬彂閫佽繖涓ご閮ㄣ€傚鏋滀綘缂栧啓鑷繁鐨?XMLHttpRequest 璋冪敤锛堝湪娴忚鍣ㄧ锛夛紝浣犲繀椤绘墜宸ヨ缃繖涓€兼潵璁?is_ajax() 鍙互宸ヤ綔銆?

銆€銆€濡傛灉涓€涓搷搴旈渶瑕佹牴鎹姹傛槸鍚︽槸閫氳繃AJAX 鍙戣捣鐨勶紝骞朵笖浣犳鍦ㄤ娇鐢ㄦ煇绉嶅舰寮忕殑缂撳瓨渚嬪Django 鐨?cache middleware锛?
   浣犲簲璇ヤ娇鐢?vary_on_headers(鈥?/span>HTTP_X_REQUESTED_WITH鈥?/span>) 瑁呴グ浣犵殑瑙嗗浘浠ヨ鍝嶅簲鑳藉姝g‘鍦扮紦瀛樸€?/pre>
鏂规硶

娉ㄦ剰锛氶敭鍊煎鐨勫€兼槸澶氫釜鐨勬椂鍊?姣斿checkbox绫诲瀷鐨刬nput鏍囩锛宻elect鏍囩锛岄渶瑕佺敤锛?/p>

request.POST.getlist("hobby")

Response瀵硅薄

涓庣敱Django鑷姩鍒涘缓鐨凥ttpRequest瀵硅薄鐩告瘮锛孒ttpResponse瀵硅薄鏄垜浠殑鑱岃矗鑼冨洿浜嗐€傛垜浠啓鐨勬瘡涓鍥鹃兘闇€瑕佸疄渚嬪寲锛屽~鍏呭拰杩斿洖涓€涓狧ttpResponse銆?/p>

HttpResponse绫讳綅浜巇jango.http妯″潡涓€?/p>

浣跨敤

浼犻€掑瓧绗︿覆

from django.http import HttpResponse
response = HttpResponse("Here鈥榮 the text of the Web page.")
response = HttpResponse("Text only, please.", content_type="text/plain")

璁剧疆鎴栧垹闄ゅ搷搴斿ご淇℃伅

response = HttpResponse()
response[鈥楥ontent-Type鈥榏 = 鈥榯ext/html; charset=UTF-8鈥?
del response[鈥楥ontent-Type鈥榏

灞炴€?/h3>

HttpResponse.content锛氬搷搴斿唴瀹?/p>

HttpResponse.charset锛氬搷搴斿唴瀹圭殑缂栫爜

HttpResponse.status_code锛氬搷搴旂殑鐘舵€佺爜

JsonResponse瀵硅薄

JsonResponse鏄疕ttpResponse鐨勫瓙绫伙紝涓撻棬鐢ㄦ潵鐢熸垚JSON缂栫爜鐨勫搷搴斻€?/p>

from django.http import JsonResponse

response = JsonResponse(鈥榝oo鈥? 鈥榖ar鈥榼)
print(response.content)

b鈥榹"foo": "bar"鈥?/pre>

榛樿鍙兘浼犻€掑瓧鍏哥被鍨嬶紝濡傛灉瑕佷紶閫掗潪瀛楀吀绫诲瀷闇€瑕佽缃竴涓媠afe鍏抽敭瀛楀弬鏁般€?/p>

response = JsonResponse([1, 2, 3], safe=False)

Django shortcut functions

瀹樻柟鏂囨。

render()

鎶€鏈浘鐗? src=

缁撳悎涓€涓粰瀹氱殑妯℃澘鍜屼竴涓粰瀹氱殑涓婁笅鏂囧瓧鍏革紝骞惰繑鍥炰竴涓覆鏌撳悗鐨?HttpResponse 瀵硅薄銆?/p>

鍙傛暟锛?/p>

  • request锛?鐢ㄤ簬鐢熸垚鍝嶅簲鐨勮姹傚璞°€?/li>
  • template_name锛氳浣跨敤鐨勬ā鏉跨殑瀹屾暣鍚嶇О锛屽彲閫夌殑鍙傛暟
  • context锛氭坊鍔犲埌妯℃澘涓婁笅鏂囩殑涓€涓瓧鍏搞€傞粯璁ゆ槸涓€涓┖瀛楀吀銆傚鏋滃瓧鍏镐腑鐨勬煇涓€兼槸鍙皟鐢ㄧ殑锛岃鍥惧皢鍦ㄦ覆鏌撴ā鏉夸箣鍓嶈皟鐢ㄥ畠銆?/li>
  • content_type锛氱敓鎴愮殑鏂囨。瑕佷娇鐢ㄧ殑MIME绫诲瀷銆傞粯璁や负 DEFAULT_CONTENT_TYPE 璁剧疆鐨勫€笺€傞粯璁や负鈥榯ext/html鈥?/li>
  • status锛氬搷搴旂殑鐘舵€佺爜銆傞粯璁や负200銆?/li>
  • useing: 鐢ㄤ簬鍔犺浇妯℃澘鐨勬ā鏉垮紩鎿庣殑鍚嶇О銆?/li>

涓€涓畝鍗曠殑渚嬪瓙锛?/p>

from django.shortcuts import render

def my_view(request):
    # 瑙嗗浘鐨勪唬鐮佸啓鍦ㄨ繖閲?
    return render(request, 鈥榤yapp/index.html鈥? 鈥榝oo鈥? 鈥榖ar鈥榼)

涓婇潰鐨勪唬鐮佺瓑浜庯細

鎶€鏈浘鐗? src=
from django.http import HttpResponse
from django.template import loader

def my_view(request):
    # 瑙嗗浘浠g爜鍐欏湪杩欓噷
    t = loader.get_template(鈥榤yapp/index.html鈥?
    c = 鈥榝oo鈥? 鈥榖ar鈥榼
    return HttpResponse(t.render(c, request))
鎶€鏈浘鐗? src=

redirect()

鍙傛暟鍙互鏄細

  • 涓€涓ā鍨嬶細灏嗚皟鐢ㄦā鍨嬬殑get_absolute_url() 鍑芥暟
  • 涓€涓鍥撅紝鍙互甯︽湁鍙傛暟锛氬皢浣跨敤urlresolvers.reverse 鏉ュ弽鍚戣В鏋愬悕绉?/span>
  • 涓€涓粷瀵圭殑鎴栫浉瀵圭殑URL锛屽皢鍘熷皝涓嶅姩鐨勪綔涓洪噸瀹氬悜鐨勪綅缃€?/span>

榛樿杩斿洖涓€涓复鏃剁殑閲嶅畾鍚戯紱浼犻€?tt class="docutils literal">permanent=True 鍙互杩斿洖涓€涓案涔呯殑閲嶅畾鍚戙€?/span>

绀轰緥:

浣犲彲浠ョ敤澶氱鏂瑰紡浣跨敤redirect() 鍑芥暟銆?/span>

浼犻€掍竴涓叿浣撶殑ORM瀵硅薄锛堜簡瑙e嵆鍙級

灏嗚皟鐢ㄥ叿浣揙RM瀵硅薄鐨?tt class="xref py py-meth docutils literal">get_absolute_url() 鏂规硶鏉ヨ幏鍙栭噸瀹氬悜鐨刄RL锛?/p>

from django.shortcuts import redirect
 
def my_view(request):
    ...
    object = MyModel.objects.get(...)
    return redirect(object)

浼犻€掍竴涓鍥剧殑鍚嶇О

def my_view(request):
    ...
    return redirect(鈥榮ome-view-name鈥? foo=鈥榖ar鈥?

浼犻€掕閲嶅畾鍚戝埌鐨勪竴涓叿浣撶殑缃戝潃

def my_view(request):
    ...
    return redirect(鈥?some/url/鈥?

褰撶劧涔熷彲浠ユ槸涓€涓畬鏁寸殑缃戝潃

def my_view(request):
    ...
    return redirect(鈥榟ttp://example.com/鈥?

榛樿鎯呭喌涓嬶紝redirect() 杩斿洖涓€涓复鏃堕噸瀹氬悜銆?span id="yiyi-59" class="yiyi-st">浠ヤ笂鎵€鏈夌殑褰㈠紡閮芥帴鏀朵竴涓?tt class="docutils literal">permanent 鍙傛暟锛?span id="yiyi-60" class="yiyi-st">濡傛灉璁剧疆涓?tt class="docutils literal">True锛屽皢杩斿洖涓€涓案涔呯殑閲嶅畾鍚戯細

def my_view(request):
    ...
    object = MyModel.objects.get(...)
    return redirect(object, permanent=True)銆€銆€

鎵╁睍闃呰锛?/strong> 

涓存椂閲嶅畾鍚戯紙鍝嶅簲鐘舵€佺爜锛?02锛夊拰姘镐箙閲嶅畾鍚戯紙鍝嶅簲鐘舵€佺爜锛?01锛夊鏅€氱敤鎴锋潵璇存槸娌′粈涔堝尯鍒殑锛屽畠涓昏闈㈠悜鐨勬槸鎼滅储寮曟搸鐨勬満鍣ㄤ汉銆?/p>

A椤甸潰涓存椂閲嶅畾鍚戝埌B椤甸潰锛岄偅鎼滅储寮曟搸鏀跺綍鐨勫氨鏄疉椤甸潰銆?/p>

A椤甸潰姘镐箙閲嶅畾鍚戝埌B椤甸潰锛岄偅鎼滅储寮曟搸鏀跺綍鐨勫氨鏄疊椤甸潰銆?/p>

以上是关于Request鍜孯esponse的主要内容,如果未能解决你的问题,请参考以下文章

Request,Request.QueryString,Request.Params,Request.Form,Request.ServerVariables之间的区别

request.getScheme() request.getServerName() request.getServerPort() request.getContextPath()(示例(代码

request.getParameter()request.getInputStream()和request.getReader()

request.getParameter() request.getInputStream()和request.getReader()三者的区别

urllib.request.Request