<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aminul Islam</title>
    <description>The latest articles on DEV Community by Aminul Islam (@aminul_islam113).</description>
    <link>https://dev.to/aminul_islam113</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2743221%2F95eeb8b4-156a-483a-9f78-200a89149be9.jpg</url>
      <title>DEV Community: Aminul Islam</title>
      <link>https://dev.to/aminul_islam113</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aminul_islam113"/>
    <language>en</language>
    <item>
      <title>Context processor in Django</title>
      <dc:creator>Aminul Islam</dc:creator>
      <pubDate>Thu, 31 Jul 2025 03:36:13 +0000</pubDate>
      <link>https://dev.to/aminul_islam113/context-processor-in-django-2ino</link>
      <guid>https://dev.to/aminul_islam113/context-processor-in-django-2ino</guid>
      <description>&lt;p&gt;In Django, a context processor is a Python function that takes a request object as an argument and returns a dictionary of data that gets added to the context of every template rendered using Django's RequestContext.&lt;/p&gt;

&lt;h2&gt;
  
  
  Purpose
&lt;/h2&gt;

&lt;p&gt;Context processors let you inject common variables into the context of all templates, without having to explicitly pass them in each view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Use Case
&lt;/h2&gt;

&lt;p&gt;Imagine you want your site to always display the logged-in user's name and the current year in the footer. Instead of passing that manually from every view, you can use a context processor.&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Define a context processor function:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# myapp/context_processors.py
from datetime import datetime

def global_variables(request):
    return {
        'current_year': datetime.now().year,
        'user_ip': request.META.get('REMOTE_ADDR'),
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add it to settings:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In settings.py, add it to TEMPLATES:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        ...
        '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',
                'myapp.context_processors.global_variables',  # &amp;lt;-- custom processor
            ],
        },
    },
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use in template:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;footer&amp;gt;
    &amp;amp;copy; {{ current_year }} | Your IP: {{ user_ip }}
&amp;lt;/footer&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Built-in Context Processors
&lt;/h2&gt;

&lt;p&gt;Some useful built-in ones:&lt;br&gt;
django.template.context_processors.request --&amp;gt; adds the request object.&lt;br&gt;
django.contrib.auth.context_processors.auth --&amp;gt; adds user, perms.&lt;br&gt;
django.template.context_processors.static --&amp;gt; adds STATIC_URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;A context processor is a handy way to share variables across all templates without manually including them in every view, helping to keep your code DRY and centralized.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
