Django: accessing session variables from within a template?

前端 未结 9 1474
陌清茗
陌清茗 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 20:57

    the simplest implementation is using if loop :

    {% if 'data' in request.session %}
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 21:05

    You need to add django.core.context_processors.request to your template context processors. Then you can access them like this:

    {{ request.session.name }}
    

    In case you are using custom views make sure you are passing a RequestContext instance. Example taken from documentation:

    from django.shortcuts import render_to_response
    from django.template import RequestContext
    
    def some_view(request):
        # ...
        return render_to_response('my_template.html',
                                  my_data_dictionary,
                                  context_instance=RequestContext(request))
    

    Update 2013: Judging by the upvotes I'm still receiving for this answer, people are still finding it helpful, more than three years after it was originally written. Please note however, that although the view code above is still valid, nowadays there is a much simpler way of doing this. render() is a function very similar to render_to_response(), but it uses RequestContext automatically, without a need to pass it explicitly:

    from django.shortcuts import render
    
    def some_view(request):
        # ...
        return render(request, 'my_template.html', my_data_dictionary)
    
    0 讨论(0)
  • 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))
    
    0 讨论(0)
  • 2020-11-28 21:13

    Continuing @Ludwik Trammer answer, How to add TEMPLATE_CONTEXT_PROCESSORS

    For django 1.6, in settings.py add TEMPLATE_CONTEXT_PROCESSORS referring the below code and then use {{ request.session.name }} in template files.

    TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.request")
    

    Reference https://docs.djangoproject.com/en/1.6/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS

    Pls note that, you should use that complete code in settings. Using "django.core.context_processors.request" alone will result in overriding the default settings.

    0 讨论(0)
  • 2020-11-28 21:17

    First print request.session.keys() then

    request.session['_auth_user_id']
    request.session['_auth_user_backend']
    

    You will get these two session variables.

    0 讨论(0)
提交回复
热议问题