Django: accessing session variables from within a template?

前端 未结 9 1554
陌清茗
陌清茗 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:10

    In your settins.py

    TEMPLATE_CONTEXT_PROCESSORS = (
        'django.core.context_processors.request',
    )
    

    Your view, maybe look like this.

    from django.shortcuts import render_to_response, render
    from django.http import HttpResponse, HttpResponseRedirect
    from django.template import RequestContext
    
    @login_required()
    def index_admin(request):
        return render_to_response('carteras/index_admin.html', {}, context_instance=RequestContext(request))
    

提交回复
热议问题