pythondjangotemplatesrequestinclude

Django, How to access request data in include template


In my Django project, I have some templates that includes in base template using {% include %} tag. How do I access request data in this included template? How do I access context variables in this included template?

I was expecting that request data is accessible in any template.


Solution

  • Make sure that django.template.context_processors.request is included in your context_processors in the TEMPLATES setting in your settings.py. This is usually enabled by default in Django.Once the context processor is enabled, you can access the request object in any template, including those included via {% include %}.

    For example, in your included template, you can access the user with {{ request.user }}.