Re: {{ perms }} seems to be empty
Jeremy Dunck wrote: > On 5/23/07, Michael Lake <[EMAIL PROTECTED]> wrote: > ... > > return render_to_response('lab/user.html', data) > > Yeah, that's your problem right there. > : > ... I imagine this bites a lot of people on the ass. :( Add me to the list of ass-bitten and extremely frustrated. I followed the tutorials through (including the part about the render_to_response() shortcut), but when I started adding permission levels to my own app, was left scratching my head when the {{perms.foo}} feature didn't work at all. (http://www.djangoproject.com/documentation/authentication/) After trying every goofy combination of dot-syntax I could think of, passing my own 'perms' to the shortcut as data (which I knew wasn't right), { 'perms': httpreq.user.get_all_permissions(), } and snooping in django/core/context_processors.py without any magical insight, I finally caved in and joined this list. And, sure enough, it wasn't just me! I'm WAY too much of a Python & Django noob to suggest design changes, but... Any chance of getting the _DOCS_ updated? For example, it would be nice if the first place {{perms.foo}} was mentioned would at least hint that it wouldn't work in conjunction with the render_to_response() shortcut, or perhaps it could introduce the RequestContext class (which I never would've _heard_ of, except for this thread). (Anyways, thanks to Jeremy for the explanation/solution, I'm at least making some progress again now that I've got my own shortcut function to replace render_to_response()) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
rskm1 wrote: > Any chance of getting the _DOCS_ updated? > > For example, it would be nice if the first place {{perms.foo}} was > mentioned would at least hint that it wouldn't work in conjunction > with the render_to_response() shortcut, or perhaps it could introduce > the RequestContext class (which I never would've _heard_ of, except > for this thread). Whoops, I'm retarded, it actually DOES introduce the RequestContext class (the significance of which didn't "sink in" until after reading this thread). It would've saved me a lot of pain, though, had it mentioned that render_to_response() did NOT use RequestContext. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
On 5/23/07, Michael Lake <[EMAIL PROTECTED]> wrote: ... > Oh I'll probably just be doing stuff like: > > {% if perms.lab.add_experiment %} > < a href="something">Click to add experiment. ... This should work. > Also I should ask: > return render_to_response('lab/user.html', data) > > was replaced with: > ctx = RequestContext(request,data) > return render_to_response('lab/user.html', context_instance=ctx) > > But why? What's happening in the later case so why did this fix my problem. render_to_response is a shortcut that makes some assumptions about the response you want to send, hence it's placement in django.shortcuts. I actually don't recommend using shortcuts until you know what you're missing, but in this case, the "normal" way of calling render_to_response is as you did: render_to_response(template_name, context_data) But this makes assumptions about how you want to load the template (based on the name), that you want to use a standard django.template.Context rather than a django.template.RequestContext, and that the stock HttpResponse, with no overrides, is what you want to return to the client. These are often good assumptions, but not in your case. TEMPLATE_CONTEXT_PROCESSORS don't run on standard Context objects. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
RE: {{ perms }} seems to be empty
> I guess you're looking for any permissions that are for the 'lab' app? > > Consider moving this into the view or writing a template tag-- There's > not an easy way to do str.startswith in a template, which is what > you'd need here. I thought you could do {% if perms.lab %} for "any permission in the lab app" ? Chris --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
Jeremy Dunck wrote: > On 5/23/07, Michael Lake <[EMAIL PROTECTED]> wrote: >>I notice it's a Set object comprising a single list. So I thought I could do >>this: >> >>{% for item in perms.lab %} >>{{ item }} >>{% endfor %} > > > Yeah, you can't do attribute access like that for a set. It's not the > same as a dictionary. > I guess you're looking for any permissions that are for the 'lab' app? Oh I'll probably just be doing stuff like: {% if perms.lab.add_experiment %} < a href="something">Click to add experiment. {% endif %} {% if perms.lab.delete_experiment %} < a href="something">Click to delete experiment. {% endif %} {% if perms.lab.change_experiment %} < a href="something">Click to edit experiment. {% endif %} Also I should ask: return render_to_response('lab/user.html', data) was replaced with: ctx = RequestContext(request,data) return render_to_response('lab/user.html', context_instance=ctx) But why? What's happening in the later case so why did this fix my problem. I suppose I'm asking for an explanation of the magic under the carpet when I'm not even a magicians apprentice and will have problems understanding the answer :-) Mike -- Michael Lake --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
On 5/23/07, Michael Lake <[EMAIL PROTECTED]> wrote: ... > I notice it's a Set object comprising a single list. So I thought I could do > this: > > {% for item in perms.lab %} > {{ item }} > {% endfor %} Yeah, you can't do attribute access like that for a set. It's not the same as a dictionary. I guess you're looking for any permissions that are for the 'lab' app? Consider moving this into the view or writing a template tag-- There's not an easy way to do str.startswith in a template, which is what you'd need here. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
Jeremy Dunck wrote: > Should be: > > from django.template import RequestContext > ctx = RequestContext(request, data) > return render_to_response('lab/user.html', context_instance=ctx) Ah ha :-) Now it works and I get... perms={% if perms.lab %} yes perms {{ perms.lab }} {% else %} no perms {% endif %} Now gives... perms= yes perms Set(['lab.change_test', 'lab.add_test', 'lab.add_experiment', 'lab.delete_experiment', 'lab.delete_test', 'lab.change_experiment']) Thanks Jeremy, Martin & Aidas for your help. I notice it's a Set object comprising a single list. So I thought I could do this: {% for item in perms.lab %} {{ item }} {% endfor %} But the above just seems to hand the browser and waits and waits... Mike -- Michael Lake --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
Arg! This: > from django.template import RequestContext > ctx = RequestContext(request) > return render_to_response('lab/user.html', context_instance=ctx) Should be: from django.template import RequestContext ctx = RequestContext(request, data) return render_to_response('lab/user.html', context_instance=ctx) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
On 5/23/07, Michael Lake <[EMAIL PROTECTED]> wrote: ... > return render_to_response('lab/user.html', data) Yeah, that's your problem right there. You want this: from django.template import RequestContext ctx = RequestContext(request) return render_to_response('lab/user.html', context_instance=ctx) (Style note, you want your imports at the top of the file or callable, but included just before use here for clarity) ... I imagine this bites a lot of people on the ass. :( --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
Hi all Martin Hsu suggested to make sure that this is included so I now have this in settings.py TEMPLATE_CONTEXT_PROCESSORS= ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n") Still {{ perms }} is empty or at least not displayed. Jeremy Dunck wrote: > You must also render the template using RequestContext rather than > Context in order for TEMPLATE_CONTEXT_PROCESSORS to be run. > How is 'request' getting into your template context? > Show us the view. :) Here is the view: def user(request): ''' Provides information on the currently logged in user. ''' message = '' # This is an object that represents the currently logged-in user. # If the user isn't logged in, this will instead be an AnonymousUser object user = request.user # How to tell if a user is logged in. if request.user.is_authenticated(): # Do something for authenticated users. debug = 'User is authenticated.' else: # Do something for anonymous users. debug = 'User is not authenticated.' return HttpResponseRedirect("/login/") # now user is authenticated, and has name == request.user.username message = 'This user ' if user.has_perm('lab.add_test'): message = message + 'can add a lab test.' else: message = message + 'cannot add any lab tests.' if user.is_authenticated(): permissions = user.get_all_permissions() else: permissions = '' data = {'request': request, 'user': user, # passing through permissions is just for testing. # I should just use {{ perms }} in the template 'permissions': permissions, 'message': message, 'debug': debug, } return render_to_response('lab/user.html', data) Here is part of the template user.html Permissions: {% for permission in permissions %} + {{ permission }} {% endfor %} perms={% if perms.lab %} yes perms {% else %} no perms {% endif %} {{ message }} - And here is the output I get: Permissions: + lab.change_test + lab.add_test + lab.add_experiment + lab.delete_experiment + lab.delete_test + lab.change_experiment perms= no perms This user can add a lab test. I could create a dictionary in the view of all the permission things I'd need in a template and pass that in but the {{ perms }} seems like it is really what I should be using as it supposed to be automatically available from the request object. -- Michael Lake --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
On 5/23/07, Aidas Bendoraitis <[EMAIL PROTECTED]> wrote: > > Is "django.core.context_processors.auth" set in your > TEMPLATE_CONTEXT_PROCESSORS? Are those new custom You must also render the template using RequestContext rather than Context in order for TEMPLATE_CONTEXT_PROCESSORS to be run. How is 'request' getting into your template context? Show us the view. :) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
> 1. TEMPLATE_CONTEXT_PROCESSORS is not in my settings file. But the docs say > that its default is: ("django.core.context_processors.auth", etc... > That is correct: http://www.djangoproject.com/documentation/settings/#template-context-processors The default isn't explicitly defined in settings.py. If you really wanted to be sure it's in there, set it. For example, TEMPLATE_CONTEXT_PROCESSORS= ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n") I think it would have been better to have it explicit set in settings.py. It would be more clear (as in your case) and easier to modify (you wouldn't have to go find the documentation and copy/paste it in). There is probably a better way to check TEMPLATE_CONTEXT_PROCESSORS, but setting it will help you eliminate this problem. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
> 1. TEMPLATE_CONTEXT_PROCESSORS is not in my settings file. But the docs say > that its default is: ("django.core.context_processors.auth", etc... > That is correct: http://www.djangoproject.com/documentation/settings/#template-context-processors The default isn't explicitly defined in settings.py. If you really wanted to be sure it's in there, set it. For example, TEMPLATE_CONTEXT_PROCESSORS= ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n") I think it would have been better to have it explicit set in settings.py. It would be more clear (as in your case) and easier to modify (you wouldn't have to go find the documentation and copy/paste it in). There is probably a better way to check TEMPLATE_CONTEXT_PROCESSORS, but setting it will help you eliminate this problem. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
> 1. TEMPLATE_CONTEXT_PROCESSORS is not in my settings file. But the docs say > that its default is: ("django.core.context_processors.auth", etc... > That is correct: http://www.djangoproject.com/documentation/settings/#template-context-processors The default isn't explicitly defined in settings.py. If you really wanted to be sure it's in there, set it. For example, TEMPLATE_CONTEXT_PROCESSORS= ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n") I think it would have been better to have it explicit set in settings.py. It would be more clear (as in your case) and easier to modify (you wouldn't have to go find the documentation and copy/paste it in). There is probably a better way to check TEMPLATE_CONTEXT_PROCESSORS, but setting it will help you eliminate this problem. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
Hi Aidas asked . > Is "django.core.context_processors.auth" set in your > TEMPLATE_CONTEXT_PROCESSORS? Are those new custom permissions in the > auth_permission table? If not - try to delete the test tables and > syncdb again. 1. TEMPLATE_CONTEXT_PROCESSORS is not in my settings file. But the docs say that its default is: ("django.core.context_processors.auth", etc... 2. Are the permissions in the auth_permissions table? I'm not really sure They are listed in the graphical admin page/users/ page when you edit a user listed there. The table shows 'Available user permissions' and 'Chosen user permissions' and I have added them to the later. I don't know if they are custom permissions - I didn't do anything explicit to add then. Django got them from my models automatically. 3. You suggested to reload the Test tables. I have done that via sqlite3 and dropped the Test table then did. $ python manage.py syncdb Creating table lab_test $ My template still gives the same output. > On 5/23/07, Michael Lake <[EMAIL PROTECTED]> wrote: > > > > Hi all > > > > I have a template and request is being passed to it and so I > expected that > > {{ perms }} would be availablke as in Chap 12 of the Django book. > > > > I have this: > > > > Permissions: > > {% for permission in in request.user.get_all_permissions %} > > + {{ permission }} > > {% endfor %} > > > > {% if perms.lab %} > > yes you have permissions > > {% else %} > > no permissions > > {% endif %} > > > > On output I get: > > > > Permissions: > >+ lab.change_test > >+ lab.delete_test > >+ lab.add_test > > no permissions > > > > > > I get the same "no permissions" if I try {% if perms.lab %} or {% > if perms %} > > > > What am I doing wrong? > > > > Mike > > > > -- > > Michael Lake > > > > > > > > > > > > > > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: {{ perms }} seems to be empty
Is "django.core.context_processors.auth" set in your TEMPLATE_CONTEXT_PROCESSORS? Are those new custom permissions in the auth_permission table? If not - try to delete the test tables and syncdb again. Regards, Aidas Bendoraitis aka Archatas On 5/23/07, Michael Lake <[EMAIL PROTECTED]> wrote: > > Hi all > > I have a template and request is being passed to it and so I expected that > {{ perms }} would be availablke as in Chap 12 of the Django book. > > I have this: > > Permissions: > {% for permission in in request.user.get_all_permissions %} > + {{ permission }} > {% endfor %} > > {% if perms.lab %} > yes you have permissions > {% else %} > no permissions > {% endif %} > > On output I get: > > Permissions: >+ lab.change_test >+ lab.delete_test >+ lab.add_test > no permissions > > > I get the same "no permissions" if I try {% if perms.lab %} or {% if perms %} > > What am I doing wrong? > > Mike > > -- > Michael Lake > > > > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---