On Thu, Aug 13, 2009 at 8:25 PM, Nicolas Aggelidis <n.aggeli...@gmail.com>wrote:
> > hi to all, i am a real newbie when it comes to django(4days). > I have the following question: > > Let's say i have the following files > > #views.py > from django.contrib.auth.models import User > > > def aview(request): > a_user = User.objects.filter(username="djanog") > users = User.objects.all() > return render_to_response('temp.html', > { > 'user' : a_user, > 'users' : users, > } > ) > > #temp.html > > <html> > <head> > <meta http-equiv="content-type" content="text/html; charset=utf-8"> > > <title>Django Entry Page -- aggelidis09</title> > > </head> > <body> > <h1 id="">Demo for user model</h1> > <p>{{ user }} has the following fields</p> > <p>s{{ user.username }}</p> > <p>{{ user.email }}</p> > <p>{{ user.last_name }}</p> > <p>{{ user.first_name }}</p> > <h1 id="">Demo for user model</h1> > {% for object in users %} > <p>A user:</p> > <p>{{ object.username }}</p> > <p>{{ object.email }}</p> > <p>{{ object.last_name }}</p> > <p>{{ object.first_name }}</p> > > {% endfor %} > </body> > </html> > > The for loop with users works as expected, meaning it prints the > requested attributes of each user. > But the invocation of user only prints the {{ user }}, but not > user.username or any other attribute... > > any ideas why is this happening?? > a_user = User.objects.filter(username="djanog") returns a QuerySet, not a single instance of a user. If you want it to be a single instance, change the 'filter' call to 'get'. As it is your {{ user }} in the template is "working" because it prints the representation of the query set, but the attempts to access the individual User attributes don't work since those are not attributes of a QuerySet. Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---