Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get request.user in a TemplateTag

How does one get in the value of request.user into a TemplateTag?

In the file myproject/news/templatetags/news_tags.py I have:

from django import template
from myproject.news.buildnews import BuildNewsList
from django.contrib.auth.models import User
from django import http
from django.contrib import admin
from django.template import RequestContext

register = template.Library()

def news_now (context):
    #who = request.user  ## this doesn't work
    news = BuildNewsList()
    entries = news.buildnewslist()
    return {'entries': entries, 'who': who, }

register.inclusion_tag('news_list.html', takes_context=True)(news_now)

Separately I have a file news_list.html and overall the templatetag works. I just haven't been able to pull in the value of request.user in this templatetag.

Would appreciate any pointers. Thanks. Kevin

like image 298
Kevin Avatar asked Dec 07 '22 00:12

Kevin


2 Answers

... it could look like this:

 u = context['request'].user
like image 87
Ben Keating Avatar answered Dec 09 '22 14:12

Ben Keating


Do you have django.core.context_processors.request in your settings.CONTEXT_PROCESSORS? If so, make the first argument to the tag the request object and then you should be fine.

like image 44
Steve Jalim Avatar answered Dec 09 '22 14:12

Steve Jalim