Django TypeError

101 views
Skip to first unread message

Sandeep kaur

unread,
Jul 17, 2012, 2:00:39 AM7/17/12
to django-users, greatdevelopers
I want to have different views for my application for different types
of users. For this I have this code for my views.py file :

def index1(request):
u = User.objects.all()
if u.is_staff ==1 and u.is_active == 1 and u.is_superuser == 1:
return render_to_response('tcc11_12/index1.html',
context_instance=RequestContext(request))
elif u.is_staff == 1 and u.is_active == 1 and u.is_superuser == 0:
return render_to_response('tcc11_12/index2.html',
context_instance=RequestContext(request))
else:
return render_to_response('index3.html',
context_instance=RequestContext(request))

But it gives TypeError as :
"cannot convert dictionary update sequence element #0 to a sequence"

Is there anything wrong in above code?

--
Sandeep Kaur
E-Mail: mkaur...@gmail.com
Blog: sandymadaan.wordpress.com

@@

unread,
Jul 17, 2012, 2:29:22 AM7/17/12
to django...@googlegroups.com
 u = User.objects.all() 
u is not a user. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Sandeep kaur

unread,
Jul 18, 2012, 4:40:28 AM7/18/12
to django...@googlegroups.com
On Tue, Jul 17, 2012 at 11:59 AM, @@ <ask...@gmail.com> wrote:
> u = User.objects.all()
> u is not a user.
>
Oh yes, thank you for pointing.
Please help me with this :
How can I get the user id or 'u' of the user who has login using
Django inbuilt authentication, so that I can give different views to
the users based on there permissions ?
Thanks in advance.

kenneth gonsalves

unread,
Jul 18, 2012, 5:43:44 AM7/18/12
to django...@googlegroups.com
On Wed, 2012-07-18 at 14:10 +0530, Sandeep kaur wrote:
> On Tue, Jul 17, 2012 at 11:59 AM, @@ <ask...@gmail.com> wrote:
> > u = User.objects.all()
> > u is not a user.
> >
> Oh yes, thank you for pointing.
> Please help me with this :
> How can I get the user id or 'u' of the user who has login using
> Django inbuilt authentication, so that I can give different views to
> the users based on there permissions ?
> Thanks in advance.

request.user
--
regards
Kenneth Gonsalves

Sandeep kaur

unread,
Jul 18, 2012, 2:11:54 PM7/18/12
to django...@googlegroups.com
On Wed, Jul 18, 2012 at 3:13 PM, kenneth gonsalves
<law...@thenilgiris.com> wrote:
> On Wed, 2012-07-18 at 14:10 +0530, Sandeep kaur wrote:

> request.user

Thank you sir , this was helpful. :)

bruno desthuilliers

unread,
Jul 23, 2012, 5:38:16 AM7/23/12
to django...@googlegroups.com, greatdevelopers
On Tuesday, July 17, 2012 8:00:39 AM UTC+2, sandy wrote:
I want to have different views for my application for different types
of users. For this I have this code for my views.py file :

def index1(request):
        u = User.objects.all()

This has been solved.
 
        if u.is_staff ==1 and u.is_active == 1 and u.is_superuser == 1:


User.is_staff, User.is_active and User.is_superuser already have a boolean value, so you don't need to test against 1 (or True etc).

        if u.is_staff  and u.is_active and u.is_superuser :


 
                return render_to_response('tcc11_12/index1.html',
context_instance=RequestContext(request))
        elif u.is_staff == 1 and u.is_active == 1 and u.is_superuser == 0:


You only have one different predicate here, so you could factor your tests:


     if u.is_staff  and u.is_active:
         if u.is_superuser :
             return render_to_response('tcc11_12/index1.html', context_instance=RequestContext(request))           
         else:

             return render_to_response('tcc11_12/index2.html', context_instance=RequestContext(request))
     else:
         return render_to_response('index3.html',context_instance=RequestContext(request))


Note that if only the template change, you could avoid multiple returns too:
    
     template = 'index3.html' # default
     if u.is_staff  and u.is_active:
         template = 'tcc11_12/index1.html' if u.is_superuser else  'tcc11_12/index2.html'
    
     return render_to_response(template,context_instance=RequestContext(request))


Also, if you're using request.user as the user *and the default authentication backend and auth form*, only "active" users should be able to log in, so your test on 'is_active' _may_ (or not !) be just useless:

https://docs.djangoproject.com/en/1.3/topics/auth/#django.contrib.auth.models.User.is_active

HTH
Reply all
Reply to author
Forward
0 new messages