Quantcast

User object available in base template

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

User object available in base template

Patrick J. Anderson

In my project, I have a root template called base.html, which all other
templates extend.

I'd like to create a simple navigation with user information, which I
want to write once and put in the base template to be shown on every page.

I'm not sure what the best way of retrieving information from User
object at the template root level is. I've read about RequestContext
object and template processors, but the docs are not very clear to me
and leave a lot to be guessed. I've followed the guides, but still I
can't get User object in my template. But then again, perhaps the
solution is simple, and I'm just too dense to see it.

Below is the very simple view code I'm testing:

from django.template import RequestContext, loader
from django.http import HttpResponse
from myproject.apps.journal.models import Post


def main(request):
     posts = Post.objects.filter(is_approved =
True).order_by('-time_added')[:5]
     t = loader.get_template('homepage.html')
     c = RequestContext({
         'latest_posts': posts,
     })
     return HttpResponse(t.render(c))


And here's a piece of template from "homepage.html":

{% block user_nav %}
<ul id="meta-nav">
{% if user %}
        <li><a href="/community/member/{{ user.username }}/">{{ user.first_name
}}</a></li>
{% else %}
        <li>Anonymous</li>
{% endif %}
</ul>
{% endblock %}


What am I doing wrong?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: User object available in base template

Patrick J. Anderson

Forgot to add my specific error:

Exception Type:   AttributeError
Exception Value: 'dict' object has no attribute 'user'
Exception Location:
/usr/lib/python2.4/site-packages/django/core/context_processors.py in
auth, line 17

Patrick J. Anderson



Patrick J. Anderson wrote:

> In my project, I have a root template called base.html, which all other
> templates extend.
>
> I'd like to create a simple navigation with user information, which I
> want to write once and put in the base template to be shown on every page.
>
> I'm not sure what the best way of retrieving information from User
> object at the template root level is. I've read about RequestContext
> object and template processors, but the docs are not very clear to me
> and leave a lot to be guessed. I've followed the guides, but still I
> can't get User object in my template. But then again, perhaps the
> solution is simple, and I'm just too dense to see it.
>
> Below is the very simple view code I'm testing:
>
> from django.template import RequestContext, loader
> from django.http import HttpResponse
> from myproject.apps.journal.models import Post
>
>
> def main(request):
>      posts = Post.objects.filter(is_approved =
> True).order_by('-time_added')[:5]
>      t = loader.get_template('homepage.html')
>      c = RequestContext({
>          'latest_posts': posts,
>      })
>      return HttpResponse(t.render(c))
>
>
> And here's a piece of template from "homepage.html":
>
> {% block user_nav %}
> <ul id="meta-nav">
> {% if user %}
> <li><a href="/community/member/{{ user.username }}/">{{ user.first_name
> }}</a></li>
> {% else %}
> <li>Anonymous</li>
> {% endif %}
> </ul>
> {% endblock %}
>
>
> What am I doing wrong?
>
>
> >
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: User object available in base template

donarb-2
In reply to this post by Patrick J. Anderson

On Oct 3, 2006, at 9:30 PM, Patrick J. Anderson wrote:

>
> def main(request):
>      posts = Post.objects.filter(is_approved =
> True).order_by('-time_added')[:5]
>      t = loader.get_template('homepage.html')
>      c = RequestContext({
>          'latest_posts': posts,
>      })
>      return HttpResponse(t.render(c))
>
> What am I doing wrong?



You have to pass the request object into the RequestContext  
constructor. Then the user object will show up in your templates.

c= RequestContext(request, { ... })

Don



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: User object available in base template

Alan Green-3
In reply to this post by Patrick J. Anderson

On 10/4/06, Patrick J. Anderson <[hidden email]> wrote:
...
> def main(request):
>      posts = Post.objects.filter(is_approved =
> True).order_by('-time_added')[:5]
>      t = loader.get_template('homepage.html')
>      c = RequestContext({
>          'latest_posts': posts,
>      })


That's it there, I think. RequestContext takes a request object as its
first parameter. Try

      c = RequestContext(request, {
          'latest_posts': posts,
      })

Cheers,

Alan

--
Alan Green
[hidden email] - http://bright-green.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: User object available in base template

Patrick J. Anderson
In reply to this post by donarb-2

Don Arbow wrote:

> On Oct 3, 2006, at 9:30 PM, Patrick J. Anderson wrote:
>> def main(request):
>>      posts = Post.objects.filter(is_approved =
>> True).order_by('-time_added')[:5]
>>      t = loader.get_template('homepage.html')
>>      c = RequestContext({
>>          'latest_posts': posts,
>>      })
>>      return HttpResponse(t.render(c))
>>
>> What am I doing wrong?
>
>
>
> You have to pass the request object into the RequestContext  
> constructor. Then the user object will show up in your templates.
>
> c= RequestContext(request, { ... })
>
> Don
>
>
>
> >
>

Thanks, guys!

I guess to create a user info box and make it available in all the
templates, I'd create a template tag and load it in the base template,
right?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Loading...