美文网首页
Django-状态保持(cookie的流程和设置)

Django-状态保持(cookie的流程和设置)

作者: 测试探索 | 来源:发表于2022-09-11 15:48 被阅读0次
  • 浏览器请求服务器是无状态的。
  • 无状态:指一次用户请求时,浏览器,服务器无法知道之前这个用户做过什么,每次请求都是一次新的请求。
  • 无状态原因:浏览器与服务器是使用socket套接字进行通信的,服务器将请求结果返回给浏览器后,会关闭当前的socket连接,而且服务器也会在处理页面完毕之后销毁页面对象。
  • 有时需要保持下来用户浏览的状态,比如用户是否登陆过,浏览过哪些商品等
  • 实现状态保持主要有两种方式。
    • 在客户端存储信息使用cookie
    • 在服务器存储信息使用Session

一、流程介绍

第一次请求,携带查询字符串
http://127.0.0.1:8000/set_cookie/?username=itcast&password=123
服务器接收到请求之后,获取username,服务器设置cookie信息,cookie信息包括username
浏览器接收到服务器的响应之后,应该把cookie保存起来
第二次之后的请求,访问http://127.0.0.1:8000 都会携带cookie信息,服务器就可以读取cookie信息,判断用户身份

1-1:设置cookie

子应用book/views.py

from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from book.models import BookInfo
import json,re

# Create your views here.

"""
第一次请求,携带查询字符串
http://127.0.0.1:8000/set_cookie/?username=itcast&password=123
服务器接收到请求之后,获取username,服务器设置cookie信息,cookid信息包括username
浏览器接收到服务器的响应之后,应该吧cookie保存起来
第二次之后的请求,访问http://127.0.0.1:8000 都会携带cookie信息,服务器就可以读取cookie信息,判断用户身份

"""
def set_cookie(request):
    # 1.获取查询字符串数据
    username = request.GET.get('username')
    # 2.服务器设置cookie信息,通过响应对象.set_cookie 方法
    response = HttpResponse('set cookie')
    # 参数key
    response.set_cookie('name',username)
    return response

book/urls.py

from django.urls import path
from book.views import create_book,shop,register,jsons,response,set_cookie,get_cookie
from django.urls import converters
from django.urls.converters import register_converter

# 第一步.定义转换器
class MobileConverter:
    # 验证数据的关键是:正则
    regex = '1[3-9]\d{9}'

    # 验证没有问题的数据,给视图函数
    def to_python(self, value):
        return value

    # def to_url(self, value):
    #     # 将匹配结果用于反向解析传值时使用
    #     return value

# 第二步.注册转换器,才能在第三步中使用
# 参数讲解
#converter :转换器类
#type_name :转换器名字
register_converter(MobileConverter,'phone')


urlpatterns = [
    path('create/',create_book),
    # <转换器名字:变量名>
    # 转换器会对变量数据进行正则验证
    # 第三步,使用
    # int为系统自带转换器,python为自定义编写的转换器
    path('<int:city_id>/<phone:mobile>/',shop),
    path('register/',register),
    path('json/',jsons),
    path('res/',response),
    path('set_cookie/',set_cookie),
]
image.png
1-2:获取cookie

book/views.py

from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from book.models import BookInfo
import json,re

# Create your views here.
def get_cookie(request):
    # 获取cookie
    print(request.COOKIES)
    return HttpResponse("get_cookie")

book/urls.py

from django.urls import path
from book.views import create_book,shop,register,jsons,response,set_cookie,get_cookie
from django.urls import converters
from django.urls.converters import register_converter

# 第一步.定义转换器
class MobileConverter:
    # 验证数据的关键是:正则
    regex = '1[3-9]\d{9}'

    # 验证没有问题的数据,给视图函数
    def to_python(self, value):
        return value

    # def to_url(self, value):
    #     # 将匹配结果用于反向解析传值时使用
    #     return value

# 第二步.注册转换器,才能在第三步中使用
# 参数讲解
#converter :转换器类
#type_name :转换器名字
register_converter(MobileConverter,'phone')


urlpatterns = [
    path('create/',create_book),
    # <转换器名字:变量名>
    # 转换器会对变量数据进行正则验证
    # 第三步,使用
    # int为系统自带转换器,python为自定义编写的转换器
    path('<int:city_id>/<phone:mobile>/',shop),
    path('register/',register),
    path('json/',jsons),
    path('res/',response),
    path('set_cookie/',set_cookie),
    path('get_cookie/',get_cookie),
]
image.png
image.png

二、cookie有效期设置

from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from book.models import BookInfo
import json,re

# Create your views here.

"""
第一次请求,携带查询字符串
http://127.0.0.1:8000/set_cookie/?username=itcast&password=123
服务器接收到请求之后,获取username,服务器设置cookie信息,cookid信息包括username
浏览器接收到服务器的响应之后,应该吧cookie保存起来

第二次之后的请求,访问http://127.0.0.1:8000 都会携带cookie信息,服务器就可以读取cookie信息,判断用户身份

"""
def set_cookie(request):
    # 1.获取查询字符串数据
    username = request.GET.get('username')
    password = request.GET.get('password')
    # 2.服务器设置cookie信息,通过响应对象.set_cookie 方法
    response = HttpResponse('set cookie')
    # cookie有效期设置:max_age 是一个秒数,从响应开始,计数的一个秒数
    response.set_cookie('name',username,max_age=60*60)
    response.set_cookie('pwd',password)
    return response
name设置了过期时间
pwd未设置过期时间

相关文章

网友评论

      本文标题:Django-状态保持(cookie的流程和设置)

      本文链接:https://www.haomeiwen.com/subject/elwtortx.html