Add context to every Django Admin page
<p>How do I add extra context to all admin webpages?</p>

<p>I use default Django Admin for my admin part of a site.</p>

<p>Here is an url entry for admin:</p>

<pre><code>urlpatterns = [
    url(r'^admin/', admin.site.urls),
]
</code></pre>

<p>And my apps register their standard view models using:</p>

<pre><code>admin.site.register(Tag, TagAdmin)
</code></pre>

<p>My problem, is that I want to display an extra field in admin template header bar and I have no idea how to add this extra context.</p>

<p>My first bet was adding it in url patterns like below:</p>

<pre><code>urlpatterns = [
    url(r'^admin/', admin.site.urls, {'mycontext': '123'}),
]
</code></pre>

<p>But that gives an error:</p>

<pre><code>TypeError at /admin/tickets/event/4/change/

change_view() got an unexpected keyword argument 'mycontext'
</code></pre>

<p>Can you give any suggestion? I really do not want to modify every AdminModel class I have to insert this context, as I need it on every admin page.</p>

<p>Thanks.</p>

<p>Found the solution, url registration has to be:</p>

<pre><code>urlpatterns = [
    url(r'^admin/', admin.site.urls, {'extra_context': {'mycontext': '123'}}),
]
</code></pre>

<p>Its a context dictionary inside of a dictionary with <code>'extra_context'</code> as a key.</p>

python django django-admin django-views