21

How can i get the current user in a django template tags? (request object is not accessible) Or how can i access to request object?

tapioco123
  • 3,235
  • 10
  • 36
  • 42
  • You can create it as context processor than creating template tags and call it always in a template. In context processor you can get the user without calling the function in a template always. – catherine Feb 11 '13 at 15:11
  • Really complicated for a one liner template tag.. Is really django so unhandly? – tapioco123 Feb 11 '13 at 15:14
  • 2
    Or try to put this in your template {{user.username}} – catherine Feb 11 '13 at 15:14
  • 1
    What do you want to show then? If it's not useful then your question is incomplete. If you really want help be nice, be more specific and give sample. – catherine Feb 11 '13 at 15:22
  • 1
    The question is pretty clear, he wants to access the current user in a template tag – msc Feb 11 '13 at 15:36
  • @msc I give him sample on how to access current user but he replied it's not really he really want. Then what he really want? I understand what his question above. – catherine Feb 11 '13 at 15:52
  • He wants to either access the current user object in a template tag, or the request object in a template tag. Not in a context processor, or as a variable in the template. – msc Feb 11 '13 at 16:00

4 Answers4

21

If you want to access the current user in a template tag, you must pass it as a parameter in the templates, like so:

{% my_template_tag user %}

Then make sure your template tag accepts this extra parameter. Check out the documentation on this topic. You should also check out simple tags.

msc
  • 3,780
  • 1
  • 22
  • 27
18

The user is always attached to the request, in your templates you can do the following:

{% if user.is_authenticated %}
{% endif %}

You don't have to specify "request" to access its content

UPDATE:

Be aware: is_authenticated() always return True for logged user (User objects), but returns False for AnonymousUser (guest users). Read here: https://docs.djangoproject.com/en/1.7/ref/contrib/auth/

daveoncode
  • 18,900
  • 15
  • 104
  • 159
  • `user.is_authenticated` always returns true so it's basically useless. – markwalker_ Jan 28 '15 at 10:26
  • no! It always return true for User (logged ones), but returns false for AnonymousUser, so is not useless! Read here carefully: https://docs.djangoproject.com/en/1.7/ref/contrib/auth/ – daveoncode Jan 29 '15 at 08:25
7

This question was already answered here:

{% if user.is_authenticated %}
    <p> Welcome '{{ user.username }}'</p>
{% else %}
    <a href="{% url django.contrib.auth.login %}">Login</a>
{% endif %}

and make sure you have the request template context processor installed in your settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.request',
...
)

Note:

  • Use request.user.get_username() in views & user.get_username in templates. Preferred over referring username attribute directly. Source
  • This template context variable is available if a RequestContext is used.
  • django.contrib.auth.context_processors.auth is enabled by default & contains the variable user
  • You do NOT need to enable django.core.context_processors.request template context processor.

Source : https://docs.djangoproject.com/en/dev/topics/auth/default/#authentication-data-in-templates

DSAnup
  • 23
  • 7
pyjavo
  • 1,598
  • 2
  • 23
  • 41
-1

Suppose you have a profile page of every registered user, and you only want to show the edit link to the owner of the profile page (i.e., if the current user is accessing his/her profile page, the user can see the edit button, but the user can't see the edit button on other user's profile page. In your html file:

<h2>Profile of {{ object.username }}</h2>
{% if object.username == user.username %}
    <a href="{% url 'profile_update' object.pk %}">Edit</a>
{% endif %}

Then your urls.py file should contain:

from django.urls import path
from .views import ProfileUpdateView
urlpatterns = [
    ...
    path('<int:pk>/profile/update', ProfileUpdateView.as_view(), name = 'profile_update'),
    ...
]

considering you have appropriate ProfileUpdateView and appropriate model