django中的cookie与session

获取cookie

request.COOKIE.GET

使用cookie

response.set-cookie

views.py

python 复制代码
from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.
def cookie_test(request):
    r = HttpResponse("hello world")
    r.set_cookie('lan', 'python')
    r.set_cookie('framework','django')
    print(request.COOKIES.get('lan'))
    print(request.COOKIES.get('framework'))
    return r

浏览器中观察cookie值

获取session

request.session.get

使用session

request.session

views.py

python 复制代码
def session_test(request):
    request.session['hello'] = 'world'
    print(request.session.get('hello'))
    return HttpResponse('hello session')

浏览器中查看session ------ 加密显示

用户登录与session
相关推荐
米高梅狮子14 小时前
03.网络类服务实践
linux·运维·服务器·网络·kubernetes·centos·openstack
June`14 小时前
网络编程时内核究竟做了什么???
linux·服务器·网络
原来是猿15 小时前
腾讯云服务器端口开放完全指南
服务器·网络·腾讯云
2301_8039346115 小时前
Go语言如何做网络爬虫_Go语言爬虫开发教程【指南】
jvm·数据库·python
WL_Aurora15 小时前
Python爬虫实战(六):新发地蔬菜价格数据采集.
爬虫·python
盲敲代码的阿豪15 小时前
Python 入门基础教程(爬虫前置版)
开发语言·爬虫·python
你的保护色15 小时前
【无标题】
java·服务器·网络
Elnaij15 小时前
Linux系统与系统编程(9)——自设计shell与基础IO
linux·服务器
weixin1997010801616 小时前
[特殊字符] 智能数据采集:数字化转型的“数据石油勘探队”(附Python实战源码)
开发语言·python
IMPYLH16 小时前
Linux 的 unexpand 命令
linux·运维·服务器·bash