對(duì)django views中 request, response的常用操作詳解
request
獲取post請(qǐng)求中的json數(shù)據(jù)
def hello(request): data = json.loads(request.body) ...
json格式還有一些 非表單序列化 的格式,都可以從 request.body 中獲取請(qǐng)求體中的數(shù)據(jù),對(duì)于ajax請(qǐng)求可以使用 request.is_ajax() 來(lái)判斷
根據(jù)請(qǐng)求的信息獲取base url(有時(shí)候服務(wù)的域名比較多,還是需要?jiǎng)討B(tài)的拼接一下url信息)
# url http://wificdn.com:8888/wxpay/qrcode2/16122010404238801544?name=lzz request.get_host() # wificdn.com:8888 request.get_full_path() # u'/wxpay/qrcode2/16122010404238801544?name=lzz' request.build_absolute_uri('/') # 'http://wificdn.com:8888/' request.build_absolute_uri('/hello') # 'http://wificdn.com:8888/hello' request.build_absolute_uri() # 'http://wificdn.com:8888/wxpay/qrcode2/16122010404238801544?name=lzz' request.path # u'/wxpay/qrcode2/16122010404238801544' request.scheme # 'http'
獲取表單中選中的 checkbox 信息, 例如checkbox的name為 checks
var_list = request.POST.getlist('checks')
返回的是個(gè)list對(duì)象,如果沒(méi)有��️返回 [] ,如果表單中沒(méi)有這個(gè)key也返回 []
response
json格式的響應(yīng) 1.8版本中已經(jīng)提供了 JsonResponse, from django.http import JsonResponse 就可以使用了,低版本的django可以參照源碼自己寫(xiě)一個(gè),幾行代碼就行了。 response 中設(shè)置 cookies 和 header
def xxxxview(request): .... resp = HttpResponseRedirect('/account/portal/?token=%s' % es) resp.set_cookie("coofilter", es, max_age=300) resp['Erya-Net-Type'] = NET_TYPE resp['Erya-Auth-Host'] = AUTH_HOST resp['Erya-Auth-Port'] = AUTH_PORT resp['Erya-Auth-Uip'] = ip resp['Erya-Auth-Token'] = es return resp
session
how to use session, 主要是get和set,和刪除
def post_comment(request, new_comment): if request.session.get('has_commented', False): return HttpResponse("You've already commented.") c = comments.Comment(comment=new_comment) c.save() request.session['has_commented'] = True return HttpResponse('Thanks for your comment!') def logout(request): try: del request.session['member_id'] except KeyError: pass return HttpResponse("You're logged out.")
cookies
def login(request): response = HttpResponseRedirect('/url/to_your_home_page') response.set_cookie('cookie_name1', 'cookie_name1_value') response.set_cookie('cookie_name2', 'cookie_name2_value') return response def logout(request): response = HttpResponseRedirect('/url/to_your_login') response.delete_cookie('cookie_name1') response.delete_cookie('cookie_name2') return response # 獲取 coo = request.COOKIES.get('coofilter') # cookies 過(guò)期時(shí)間 hr.set_cookie('user_id', user_id, max_age=300)
以上這篇對(duì)django views中 request, response的常用操作詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?Excel操作從零學(xué)習(xí)掌握openpyxl用法
這篇文章主要為大家介紹了Python?Excel操作從零學(xué)習(xí)掌握openpyxl用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08python數(shù)據(jù)庫(kù)操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))
這篇文章主要介紹了python數(shù)據(jù)庫(kù)操作常用功能使用方法:獲取mysql版本、創(chuàng)建表、插入數(shù)據(jù)、slect獲取數(shù)據(jù)等,下面看示例吧2013-12-12解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問(wèn)題
今天小編就為大家分享一篇解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02使用PyV8在Python爬蟲(chóng)中執(zhí)行js代碼
PyV8是chrome用來(lái)執(zhí)行javascript的引擎,據(jù)說(shuō)是最快的js引擎,通過(guò)pyv8的封裝,可以在python中使用。下面這篇文章主要介紹了使用PyV8在Python爬蟲(chóng)中執(zhí)行js代碼的相關(guān)資料,需要的朋友可以參考下。2017-02-02tensorflow學(xué)習(xí)筆記之簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)訓(xùn)練和測(cè)試
這篇文章主要為大家詳細(xì)介紹了tensorflow學(xué)習(xí)筆記,用簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)來(lái)訓(xùn)練和測(cè)試,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04uwsgi+nginx部署Django項(xiàng)目操作示例
這篇文章主要介紹了uwsgi+nginx部署Django項(xiàng)目操作,結(jié)合實(shí)例形式簡(jiǎn)單介紹了uwsgi的概念、原理、安裝、項(xiàng)目創(chuàng)建、配置、調(diào)試運(yùn)行等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12解析Python 偏函數(shù)用法全方位實(shí)現(xiàn)
這篇文章主要介紹了解析Python 偏函數(shù)用法全方位實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06