260π
You need to add django.template.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)
28π
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.
- [Django]-How to delete project in django
- [Django]-How can I list urlpatterns (endpoints) on Django?
- [Django]-Django rest framework: query parameters in detail_route
14π
I am using Django 1.9 (March 2016) and to get {{ request.session.name}}
to work, my settings have this::
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
The difference from the previous answers is: 'django.core.context_processors.request'
became 'django.template.context_processors.request'
- [Django]-Django: How can I create a multiple select form?
- [Django]-Django order_by query set, ascending and descending
- [Django]-Django multiple template inheritance β is this the right style?
10π
You can pass a request
variable to a template and there use:
{{ request.session.name }}
- [Django]-How to run a celery worker with Django app scalable by AWS Elastic Beanstalk?
- [Django]-Django/DRF β 405 Method not allowed on DELETE operation
- [Django]-How do you Serialize the User model in Django Rest Framework
9π
the simplest implementation is using if loop :
{% if 'data' in request.session %}
- [Django]-How do I perform HTML decoding/encoding using Python/Django?
- [Django]-How can I list urlpatterns (endpoints) on Django?
- [Django]-Strings won't be translated in Django using format function available in Python 2.7
3π
First print request.session.keys()
then
request.session['_auth_user_id']
request.session['_auth_user_backend']
You will get these two session variables.
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
- [Django]-Allowing only super user login
- [Django]-Django ignores router when running tests?
2π
In your settins.py
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth'
)
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))
- [Django]-How to server HTTP/2 Protocol with django
- [Django]-Resource temporarily unavailable using uwsgi + nginx
- [Django]-Change a Django form field to a hidden field
2π
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.
- [Django]-No module named MySQLdb
- [Django]-Django.contrib.gis.db.backends.postgis vs django.db.backends.postgresql_psycopg2
- [Django]-Django index page best/most common practice
1π
Maybe a bit too late now. If you directly set TEMPLATE_CONTEXT_PROCESSORS
in settings.py
, you will lose all default TEMPLATE_CONTEXT_PROCESSORS
value. Here is what I do in my settings.py
:
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as DEFAULT_TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_TEMPLATE_CONTEXT_PROCESSORS + (
'django.core.context_processors.request',
)
- [Django]-Django: How to format a DateField's date representation?
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
- [Django]-Select distinct values from a table field
1π
**
This answer is for those using Django 3.0 and above
**
You donβt need to pass the session variables from the views as the sessions are stored in django_sessions table in your database. So just use return redirect('/')
to simulate passing none without errors.
In index.html file:
-
If thereβs only single data in the session variable, use
{{ request.session.your_session_varaible_name }}
-
If you have multiple data in the session variable like a list, it can be accessed by
{% if 'cart_prod' in request.session %} {% for cart_products in request.session.cart_prod %}
<tr>
<td style="padding: 25px">
{{ cart_products }}
</td>
<tr>
{% endfor %} {% endif %}
- [Django]-Why doesn't django's model.save() call full_clean()?
- [Django]-ForeignKey to abstract class (generic relations)
- [Django]-Django TypeError: get() got multiple values for keyword argument 'invoice_id'
- [Django]-Handling race condition in model.save()
- [Django]-Django rest framework lookup_field through OneToOneField
- [Django]-Django 1.7 β App 'your_app_name' does not have migrations