Django: accessing session variables from within a template?

前端 未结 9 1533
陌清茗
陌清茗 2020-11-28 20:23

If I set a session variable in Django, like:

request.session[\"name\"] = \"name\"

Is there a way I can access it from within a template, o

9条回答
  •  一向
    一向 (楼主)
    2020-11-28 21:01

    request.session is a dictionary like any other, so you just use the normal template mechanism for attributes and members:

    {{ request.session.name }}
    

    Don't forget to pass the request into the template context, or even better ensure you are using RequestContext and have the request context processor enabled. See the documentation.

提交回复
热议问题