<p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>

<p>In my template setup, I have base.html, and the other pages extend this. I want to show logged in or register button in the base.html, but how can I ensure that the necessary variables are always a part of the context? It seems that each view just sets up the context as they like, and there is no global context population. Is there a way of doing this without including the user in each context creation?</p>

<p>Or will I have to make my own custom shortcuts to setup the context properly?</p>

[{'answer_id': 41555, 'author': 'David Webb', 'author_id': 3171, 'author_profile': 'https://Stackoverflow.com/users/3171', 'pm_score': 2, 'selected': False, 'text': '<p>If you can hook your authentication into the <a href="http://www.djangoproject.com/documentation/authentication/#authentication-in-web-requests" rel="nofollow noreferrer">Django authentication scheme</a> you\'ll be able to use <code>request.user</code>.</p>\n\n<p>I think this should just be a case of calling <code>authenticate()</code> and <code>login()</code> based on the contents of your Cookie.</p>\n\n<p>Edit: @Staale - I always use the <a href="http://www.djangobook.com/en/1.0/chapter04/" rel="nofollow noreferrer"><code>locals()</code> trick</a> for my context so all my templates can see <code>request</code> and so <code>request.user</code>.  If you\'re not then I guess it wouldn\'t be so straightforward.</p>\n'}, {'answer_id': 41558, 'author': 'Ryan Duffield', 'author_id': 2696, 'author_profile': 'https://Stackoverflow.com/users/2696', 'pm_score': 5, 'selected': True, 'text': '<p>In a more general sense of not having to explicitly set variables in each view, it sounds like you want to look at writing your own <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors" rel="noreferrer">context processor</a>.</p>\n\n<p>From the docs:</p>\n\n<blockquote>\n  <p>A context processor has a very simple interface: It\'s just a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Each context processor must return a dictionary.</p>\n</blockquote>\n'}, {'answer_id': 41560, 'author': 'Staale', 'author_id': 3355, 'author_profile': 'https://Stackoverflow.com/users/3355', 'pm_score': 2, 'selected': False, 'text': '<p>@Dave\nTo use {{user.username}} in my templates, I will then have to use \nrequestcontext rather than just a normal map/hash: <a href="http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext" rel="nofollow noreferrer">http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext</a></p>\n\n<p>So I guess there are no globals that the template engine checks.</p>\n\n<p>But the RequestContext has some prepopulate classes that I can look into to solve my problems. Thanks.</p>\n'}, {'answer_id': 269249, 'author': 'zalun', 'author_id': 23457, 'author_profile': 'https://Stackoverflow.com/users/23457', 'pm_score': 5, 'selected': False, 'text': "<p>@Ryan: Documentation about preprocessors is a bit small</p>\n\n<p>@Staale: Adding user to the Context every time one is calling the template in view, DRY</p>\n\n<p>Solution is to use a preprocessor</p>\n\n<p><strong>A</strong>: In your settings add</p>\n\n<pre><code>TEMPLATE_CONTEXT_PROCESSORS = (\n    'myapp.processor_file_name.user',\n)\n</code></pre>\n\n<p><strong>B</strong>: In myapp/processor_file_name.py insert</p>\n\n<pre><code>def user(request):\n    if hasattr(request, 'user'):\n        return {'user':request.user }\n    return {}\n</code></pre>\n\n<p>From now on you're able to use user object functionalities in your templates.</p>\n\n<pre><code>{{ user.get_full_name }}\n</code></pre>\n"}, {'answer_id': 1064621, 'author': 'Daniel', 'author_id': 50841, 'author_profile': 'https://Stackoverflow.com/users/50841', 'pm_score': 6, 'selected': False, 'text': '<p>There is <strong>no need</strong> to write a context processor for the user object if you already have the <a href="https://docs.djangoproject.com/en/dev/topics/auth/default/#authentication-data-in-templates" rel="noreferrer"><code>"django.core.context_processors.auth"</code></a> in <a href="https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS" rel="noreferrer"><code>TEMPLATE_CONTEXT_PROCESSORS</code></a> <strong>and</strong> if you\'re using <a href="https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext" rel="noreferrer"><code>RequestContext</code></a> in your views. </p>\n\n<p>if you are using django 1.4 or latest the module has been moved to <code>django.contrib.auth.context_processors.auth</code></p>\n'}, {'answer_id': 4815619, 'author': 'Anto Binish Kaspar', 'author_id': 592065, 'author_profile': 'https://Stackoverflow.com/users/592065', 'pm_score': 1, 'selected': False, 'text': '<p>its possible by default, by doing the following steps, ensure you have added the context \'django.contrib.auth.context_processors.auth\' in your settings. By default its added in settings.py, so its looks like this </p>\n\n<pre><code>TEMPLATE_CONTEXT_PROCESSORS = (\n\'django.core.context_processors.request\',\n\'django.contrib.auth.context_processors.auth\',\n\'django.core.context_processors.auth\',)\n</code></pre>\n\n<p>And you can access user object like this,</p>\n\n<pre><code>{% if user.is_authenticated %}\n&lt;p&gt;Welcome, {{ user.username }}. Thanks for logging in.&lt;/p&gt;\n{% else %}\n    &lt;p&gt;Welcome, new user. Please log in.&lt;/p&gt;\n{% endif %}\n</code></pre>\n\n<p>For more information, refer here <a href="http://docs.djangoproject.com/en/1.2/topics/auth/#authentication-data-in-templates" rel="nofollow">http://docs.djangoproject.com/en/1.2/topics/auth/#authentication-data-in-templates</a> </p>\n'}, {'answer_id': 11878636, 'author': 'iutinvg', 'author_id': 444966, 'author_profile': 'https://Stackoverflow.com/users/444966', 'pm_score': 2, 'selected': False, 'text': '<p>The hints are in every answer, but once again, from "scratch", for newbies:</p>\n\n<p>authentication data is in templates (almost) by default -- with a small trick:</p>\n\n<p>in <code>views.py</code>:</p>\n\n<pre><code>from django.template import RequestContext\n...\ndef index(request):\n    return render_to_response(\'index.html\', \n                              {\'var\': \'value\'},\n                              context_instance=RequestContext(request))\n</code></pre>\n\n<p>in <code>index.html</code>:</p>\n\n<pre><code>...\nHi, {{ user.username }}\nvar: {{ value }}\n... \n</code></pre>\n\n<p>From here: <a href="https://docs.djangoproject.com/en/1.4/topics/auth/#authentication-data-in-templates" rel="nofollow">https://docs.djangoproject.com/en/1.4/topics/auth/#authentication-data-in-templates</a></p>\n\n<blockquote>\n  <p>This template context variable is not available if a <strong>RequestContext</strong> is\n  not being used.</p>\n</blockquote>\n'}, {'answer_id': 58683713, 'author': 'synkc', 'author_id': 11078019, 'author_profile': 'https://Stackoverflow.com/users/11078019', 'pm_score': 0, 'selected': False, 'text': '<p>Use context_processors. <a href="https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-TEMPLATES-OPTIONS" rel="nofollow noreferrer">https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-TEMPLATES-OPTIONS</a></p>\n\n<blockquote>\n  <p>settings.py</p>\n</blockquote>\n\n<pre><code>\'OPTIONS\': {\n        \'context_processors\': [\n            \'django.template.context_processors.debug\',\n            \'django.template.context_processors.request\',\n            \'django.contrib.auth.context_processors.auth\',\n            \'django.contrib.messages.context_processors.messages\',\n            \'myapp.functions.test\'\n        ],\n},\n</code></pre>\n\n<blockquote>\n  <p>myapp/functions.py</p>\n</blockquote>\n\n<pre><code>def test(request):\n    return {\'misc\': \'misc\'}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/41547', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3355/']