Django-session-redis

1.安装django-redis-sessions

pip install django-redis-sessions

注:前提的linux或win安装好redis服务端

2.配置setting文件

SESSION_ENGINE = ‘redis_sessions.session’
SESSION_REDIS_HOST = ‘localhost’
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 0
SESSION_REDIS_PASSWORD = ”
SESSION_REDIS_PREFIX = ‘session’

3.其他操作访问按上篇

django-session

4.查看redis

 

5.django存储的是base64方式,可解密

 

 

Django-session

1.创建app

python manage.py startapp booktest

2.setting添加booktest

3.配置跟目录的文件夹中的urls

from django.contrib import admin
from django.urls import path, re_path
from django.conf.urls import include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('booktest/', include('booktest.urls', 'booktest')),
]

4.配置booktest/urls

from django.urls import path , re_path
from . import views

urlpatterns = [

    path('session1/', views.session1),
    path('session2/', views.session2),
    path('session2_handle/', views.session2_handle),
    path('session3/', views.session3),

]

注:最新版本2.0以上使用path 或者re_path

 

5.编写视图

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from booktest.models import BookInfo
import datetime
import logging



# 通过用户登录练习session
def session1(request):
    uname = request.session.get('myname', '未登录')
    context = {'uname': uname}
    return render(request, 'booktest/session1.html', context)


def session2(request):
    return render(request, 'booktest/session2.html')


# 提交保存session
def session2_handle(request):
    uname = request.POST['uname']
    # logging.debug('获取用户名: %s' % (uname))
    request.session['myname'] = uname
    request.session.set_expiry(0)
    return redirect('/booktest/session1/')


def session3(request):
    # 删除session
    del request.session['myname']
    return redirect('/booktest/session1/')

注:cookies写入把注释打开!

 

5.post的静态页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Title</title>
</head>
<body>
<form method="post" action="/booktest/session2_handle/">
    {% csrf_token %}
    <input type="text" name="uname">
    <input type="submit" name="登录">
</form>
</body>
</html>

注:防注入一定要添加{% csrf_token %}

 

5.访问网址

http://127.0.0.1:8000/booktest/session1/

 

 

 

 

出自于:GerryBlog

Django-request、response、重定向

1.创建app

python manage.py startapp booktest

2.setting添加booktest

3.配置跟目录的文件夹中的urls

from django.urls import path , re_path
from . import views

urlpatterns = [
    path('', views.index),
    path('<int:id>/', views.show),
    re_path(r'^detail/(?P<p1>[0-9]{4})/(?P<p2>[0-9]{2})/(?P<p3>[0-9]{2})/$', views.detail),
    path('getTest1/', views.getTest1),
    path('getTest2/', views.getTest2),
    path('getTest3/', views.getTest3),
    path('postTest1/', views.postTest1),
    path('postTest2/', views.postTest2),

    path('cookieTest/', views.cookieTest),

    path('redTest1/', views.redTest1),
    path('redTest2/', views.redTest2),
]

4.配置booktest/urls

from django.urls import path , re_path
from . import views

urlpatterns = [
    path('', views.index),
    re_path(r'^detail/(?P<p1>[0-9]{4})/(?P<p2>[0-9]{2})/(?P<p3>[0-9]{2})/$', views.detail)
]

注:最新版本2.0以上使用path 或者re_path

 

5.编写视图

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from booktest.models import BookInfo
import datetime
import logging


# Create your views here.
def index(request):
    # return HttpResponse('Hello World!')
    # temp = loader.get_template('booktest/index.html')
    # return HttpResponse(temp.render())
    # bookList = BookInfo.objects.all()
    # nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    # BookInfo.books2.create('使用类对象方式创建', nowTime).save()
    bookList = BookInfo.books2.all()
    context = {'list': bookList}
    return render(request, 'booktest/index.html', context)


def show(request, id):
    book = BookInfo.books2.get(pk=id)
    herolist = book.heroinfo_set.all()
    context = {'list': herolist}
    return render(request, 'booktest/show.html', context)


def detail(request, p1, p2, p3):
    return HttpResponse('year:%s,month:%s,day:%s' % (p1, p2, p3))


def getTest1(request):
    # 根据键接收值
    # a1 = request.GET['a']
    # logging.debug('调试: %s' % (a1))
    return render(request, 'booktest/getTest1.html')


# 接收一键一值的情况
def getTest2(request):
    # 根据键接收值
    a1 = request.GET['a']
    b1 = request.GET['b']
    c1 = request.GET['c']
    # 构造上下文
    context = {'a': a1, 'b': b1, 'c': c1}
    # 向模板中传递上下文,并进行渲染
    return render(request, 'booktest/getTest2.html', context)


# 接收一键多值的情况
def getTest3(request):
    a1 = request.GET.getlist('a')
    context = {'a': a1}
    return render(request, 'booktest/getTest3.html', context)


def postTest1(request):
    return render(request, 'booktest/postTest1.html')


def postTest2(request):
    uname = request.POST['uname']
    upwd = request.POST['upwd']
    ugender = request.POST.get('ugender')
    uhobby = request.POST.getlist('uhobby')
    context = {'uname': uname, 'upwd': upwd, 'ugender': ugender, 'uhobby': uhobby}
    return render(request, 'booktest/postTest2.html', context)


# cookie练习
def cookieTest(request):
    response = HttpResponse()
    cookie = request.COOKIES
    # logging.debug('调试: %s' % (cookie))
    # py3不存在has_key方法,改一下方法
    if 't1' in cookie:
        # logging.debug('存在t1: %s' % (cookie['t1']))
        response.write(cookie['t1'])
    # response.set_cookie('t1', 'abc')
    return response


# 转向练习
def redTest1(request):
    # return HttpResponseRedirect('/booktest/redTest2/')
    return redirect('/booktest/redTest2/')


def redTest2(request):
    return HttpResponse('这是转向来的页面')

注:cookies写入把注释打开!

 

5.访问网址

http://127.0.0.1:8000/booktest/getTest1/

http://127.0.0.1:8000/booktest/postTest1/

http://127.0.0.1:8000/booktest/cookieTest/

http://127.0.0.1:8000/booktest/redTest1/

注:打开浏览器调试界面,查看Application/cookies

 

 

 

出自于:GerryBlog