對django views中 request, response的常用操作詳解
request
獲取post請求中的json數(shù)據(jù)
def hello(request): data = json.loads(request.body) ...
json格式還有一些 非表單序列化 的格式,都可以從 request.body 中獲取請求體中的數(shù)據(jù),對于ajax請求可以使用 request.is_ajax() 來判斷
根據(jù)請求的信息獲取base url(有時候服務(wù)的域名比較多,還是需要動態(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')
返回的是個list對象,如果沒有��️返回 [] ,如果表單中沒有這個key也返回 []
response
json格式的響應(yīng) 1.8版本中已經(jīng)提供了 JsonResponse, from django.http import JsonResponse 就可以使用了,低版本的django可以參照源碼自己寫一個,幾行代碼就行了。 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 過期時間 hr.set_cookie('user_id', user_id, max_age=300)
以上這篇對django views中 request, response的常用操作詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?Excel操作從零學(xué)習(xí)掌握openpyxl用法
這篇文章主要為大家介紹了Python?Excel操作從零學(xué)習(xí)掌握openpyxl用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08python數(shù)據(jù)庫操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))
這篇文章主要介紹了python數(shù)據(jù)庫操作常用功能使用方法:獲取mysql版本、創(chuàng)建表、插入數(shù)據(jù)、slect獲取數(shù)據(jù)等,下面看示例吧2013-12-12解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問題
今天小編就為大家分享一篇解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02tensorflow學(xué)習(xí)筆記之簡單的神經(jīng)網(wǎng)絡(luò)訓(xùn)練和測試
這篇文章主要為大家詳細介紹了tensorflow學(xué)習(xí)筆記,用簡單的神經(jīng)網(wǎng)絡(luò)來訓(xùn)練和測試,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04解析Python 偏函數(shù)用法全方位實現(xiàn)
這篇文章主要介紹了解析Python 偏函數(shù)用法全方位實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06