Giter Club home page Giter Club logo

django_token_session's Introduction

django_token_session

像使用django的web session一样使用token对客户端进行会话管理

简述

web开发时,通常使用cookie来实现浏览器的会话管理,django已经对session提供了很好的支持。
当需要对不支持cookie的客户端进行会话管理时,我们通常可以在客户端和服务端之间通过token来实现。
在服务端基于django session可以非常方便的实现token会话管理,功能完备且与django session的使用方式一样。

使用

将token_middleware配置到django settings.py中。
客户端每次请求在header的Access-Token字段中带上token。
token_middleware会更加token_session的状态自动保存所做的修改。

  • 为客户端分配token:
def insert_session_view(request):  
    request.token_session.create_token()
    ...
    
    return JsonResponse({'token': request.token_session.token})
  • 在view中为token session添加内容:
def insert_session_view(request):  
    request.token_session['my_session_key'] = 'my_session_value'
    ...
  • 在view中获取token session中的内容:
def get_session_view(request):  
    my_session_value = request.token_session['my_session_key']
    ...
  • 在view中删除token session中的内容:
def delete_session_view(request):  
    del request.token_session['my_session_key']
    ...

django_token_session's People

Contributors

luoyufu avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.