20👍
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 context processor.
From the docs:
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.
46👍
There is no need to write a context processor for the user object if you already have the "django.core.context_processors.auth"
in TEMPLATE_CONTEXT_PROCESSORS
and if you’re using RequestContext
in your views.
if you are using django 1.4 or latest the module has been moved to django.contrib.auth.context_processors.auth
- [Django]-Is there a built-in login template in Django?
- [Django]-Automatic creation date for Django model form objects
- [Django]-Django Model Field Default Based Off Another Field in Same Model
33👍
@Ryan: Documentation about preprocessors is a bit small
@Staale: Adding user to the Context every time one is calling the template in view, DRY
Solution is to use a preprocessor
A: In your settings add
TEMPLATE_CONTEXT_PROCESSORS = (
'myapp.processor_file_name.user',
)
B: In myapp/processor_file_name.py insert
def user(request):
if hasattr(request, 'user'):
return {'user':request.user }
return {}
From now on you’re able to use user object functionalities in your templates.
{{ user.get_full_name }}
- [Django]-Django South – table already exists
- [Django]-Token Authentication for RESTful API: should the token be periodically changed?
- [Django]-Received "ValueError: Found wrong number (0) of constraints for …" during Django migration
3👍
The hints are in every answer, but once again, from “scratch”, for newbies:
authentication data is in templates (almost) by default — with a small trick:
in views.py
:
from django.template import RequestContext
...
def index(request):
return render_to_response('index.html',
{'var': 'value'},
context_instance=RequestContext(request))
in index.html
:
...
Hi, {{ user.username }}
var: {{ value }}
...
From here: https://docs.djangoproject.com/en/1.4/topics/auth/#authentication-data-in-templates
This template context variable is not available if a RequestContext is
not being used.
- [Django]-No Module named django.core
- [Django]-Django middleware difference between process_request and process_view
- [Django]-How can I use redis with Django?
2👍
If you can hook your authentication into the Django authentication scheme you’ll be able to use request.user
.
I think this should just be a case of calling authenticate()
and login()
based on the contents of your Cookie.
Edit: @Staale – I always use the locals()
trick for my context so all my templates can see request
and so request.user
. If you’re not then I guess it wouldn’t be so straightforward.
- [Django]-Django REST Framework – Separate permissions per methods
- [Django]-How to obtain and/or save the queryset criteria to the DB?
- [Django]-Django auto_now and auto_now_add
2👍
@Dave
To use {{user.username}} in my templates, I will then have to use
requestcontext rather than just a normal map/hash: http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext
So I guess there are no globals that the template engine checks.
But the RequestContext has some prepopulate classes that I can look into to solve my problems. Thanks.
- [Django]-Unable to find a locale path to store translations for file __init__.py
- [Django]-PyCharm Not Properly Recognizing Requirements – Python, Django
- [Django]-How do I use CommaSeparatedIntegerField in django?
1👍
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
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.auth',)
And you can access user object like this,
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
For more information, refer here http://docs.djangoproject.com/en/1.2/topics/auth/#authentication-data-in-templates
- [Django]-Detect django testing mode
- [Django]-How do I remove Label text in Django generated form?
- [Django]-Managing static files for multiple apps in Django
0👍
Use context_processors. https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-TEMPLATES-OPTIONS
settings.py
'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.functions.test'
],
},
myapp/functions.py
def test(request):
return {'misc': 'misc'}
- [Django]-What is the SQL ''LIKE" equivalent on Django ORM queries?
- [Django]-How to modify Django admin filter's title
- [Django]-Download a remote image and save it to a Django model