在book应用文件夹的views.py文件,编写视图函数,代码如下:
from django.shortcuts import renderfrom .forms import BookInfoFormfrom django.http import HttpResponse# Create your views here.def bookform(request): if request.method=='GET': bk = BookInfoForm() request.session['my'] = 'hi' return render(request,'bookinfo.html',{'bk':bk}) else: response= HttpResponse('1') print(request.session.get('my')) response.set_cookie('hello','world',60) return response
1、这里在GET请求时候设置session,在POST请求时读取session,浏览器的
Cookie中保存sessionID
2、在POST请求时候设置一个Cookie