4

I want to verify user permission in template. If a user has permission he/she will be able to access the template. After writing the below code and I granted the user permission, when I view the page it will fall on the {% else %} statement. showing that the user don't have permission. How can I go about this?

#CREATED A GROUP IN DJANGO ADMIN CALLED 'Premium'

Class Paid(models.Model):
    #models here

    class Meta:
        permissions=(
             ("view_film","Can view film"),
        )

view

def eyo(request):
    return render_to_response('eyo.html',context_instance=RequestContext(request))

template

{% block content %}

  {% if perms.paid.can_view_film %}

      <form action='https://www.test.com/checkout' method='post'>
      <input name='submit' type='submit' value='Checkout' />
     </form>

   {% else %}

    <p> yo broke! </p>

  {% endif %}
picomon
  • 1,479
  • 4
  • 21
  • 38

2 Answers2

3

Are you passing perms in your template? Are you setting perms.paid.can_view_film either explicitly in your view or via the admin interface? Is the user a part of a group that has the perms.paid.can_view_film permission?

Are you sure the app name is 'paid'? That's supposed to be the app name, not the model name.

Django Perms

Sid
  • 7,511
  • 2
  • 28
  • 41
2

I'm doing some conditional rendering based on permissions in a Django project I'm currently working on. A small example of this is a particular icon. Basically if a user has a delete permission, they see one icon, if they don't, they see another. This is how it's done in my templete:

{% if perms.List.can_delete_list %}
    <li><a href="/social/a/search/" class="settings-edit"><span class="fui-search"></span></a></li>
{% else %}
    <li><a href="{% url 'dashboard_edit' %}" class="settings-edit"><span class="fui-new"></span></a></li>
{% endif %}

If the logged in user has can_delete_list, they view one thing. If not, they view something else. Does this help?

Chris Clouten
  • 1,075
  • 3
  • 11
  • 24
  • I should add that `List` is the class here. – Chris Clouten Jun 06 '13 at 18:38
  • That's what I did above. Just that the paid is not capitalized. And I changed the paid to Paid, yet it's not working. – picomon Jun 06 '13 at 18:52
  • Did you manually grant the user permission through the Django Admin? If so, shut down the server and restart it. I've had trouble with permission based views because of that before. My second suggestion would be to try using the built in Django permissions to make sure you can get it to work before using a custom permission. In my example, `can_delete_list` is an auto generated permission by Django. – Chris Clouten Jun 06 '13 at 19:08