1. 程式人生 > >django設定並獲取cookie/session,檔案上傳,ajax接收檔案,post/get請求及跨域請求等的方法

django設定並獲取cookie/session,檔案上傳,ajax接收檔案,post/get請求及跨域請求等的方法

django設定並獲取cookie/session,檔案上傳,ajax接收檔案等的方法:

views.py檔案:

from django.shortcuts import render,HttpResponse,redirect
import datetime
import json
from .setMongodb import pinglun
from bson import ObjectId

# Create your views here.


def index(request):
    # print(render(request,'index.html
').__dict__['_container'][0]) print(render(request, 'index.html').content) return render(request, 'index.html') def product(request,id): return render(request, 'product.html',{'pid':id}) def article(request,aid): return render(request, 'article.html', {'aid': aid}) #ajax返回資料 def jsonApi(request): dictobj
= {'title': '標題'} a = HttpResponse(json.dumps(dictobj, ensure_ascii=False)) # 允許你的域名來獲取我的資料 a['Access-Control-Allow-Origin'] = "*" # 允許你攜帶Content-Type請求頭 a['Access-Control-Allow-Headers'] = "Content-Type" return a #GET/POST def methodApi(request): # http = '
http://localhost:8000/methodApi?abc=123' if request.method=='GET': request.GET.get('abc') # ===>123 request.POST.get('username') # ===>admin return HttpResponse('methodApi') #設定cookie def setcookie(request): a = HttpResponse('12345') # 設定cookies超過10秒失效,寫法 a.set_cookie('isLogin', 'true', max_age=60) # 從登入10秒後失效,寫法 current_time = datetime.datetime.utcnow() current_data = current_time + datetime.timedelta(seconds=10) a.set_cookie('key', 'value', expires=current_data) return a #獲取cookie的寫法 def getcookie(request): islogin = request.COOKIES.get('isLogin') return HttpResponse(redirect('index')) #設定session def setsession(request): request.session['username'] = '老王' return HttpResponse('setsession') #獲取seseion def getsession(request): # request.session.get('username') return HttpResponse(request.session['username']) # 設定檔案上傳 def uploadfile(request): if request.method == 'GET': return render(request, 'upload.html') else: obj = request.FILES.get('files') print(obj) print(obj.__dict__) # print(obj.name,obj.size) #讀取檔名稱和大小,返回後臺 f = open('./static/upload/'+obj.name, 'wb') for chunk in obj.chunks(): f.write(chunk) f.close() return HttpResponse('圖片上傳成功') def pinglunList(request): result = pinglun.find() resList = [] for item in result: item['_id'] = str(item['_id']) resList.append(item) # print(resList) jsonStr = json.dumps(resList,ensure_ascii=False) # print(jsonStr) a = HttpResponse(jsonStr) a['Access-Control-Allow-Origin'] = "*" # 允許你攜帶Content-Type請求頭 a['Access-Control-Allow-Headers'] = "Content-Type" return a def addContent(request): print(request.GET.get('username')) contentDict = { 'username': request.GET.get('username'), 'headerimg': request.GET.get('headerimg'), 'content': request.GET.get('content'), 'zanNum': request.GET.get('zanNum') } print(contentDict) pinglun.insert(contentDict) a = HttpResponse('ok') a['Access-Control-Allow-Origin'] = "*" # 允許你攜帶Content-Type請求頭 a['Access-Control-Allow-Headers'] = "Content-Type" return a def addzan(request): print(request.GET.get('_id')) _id = request.GET.get('_id') # res1 = pinglun.find_one({'_id': _id}) res = pinglun.update({'_id': ObjectId(_id)}, {'$inc': {'zanNum': 1}}) print(res) a = HttpResponse('ok') a['Access-Control-Allow-Origin'] = "*" # 允許你攜帶Content-Type請求頭 a['Access-Control-Allow-Headers'] = "Content-Type" return a