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 %}
4
  • Please post your view
    – Sid
    Jun 6, 2013 at 18:10
  • Please have a look into the similar question stackoverflow.com/questions/9469590/… Jun 6, 2013 at 18:14
  • Ansh, I did that yet no success!
    – picomon
    Jun 6, 2013 at 18:33
  • In your view or your custom admin view, make sure to pass request object or context, sometimes while customizing views context is not passed and perms is not well populated.
    – Fer Mena
    Sep 14, 2017 at 17:09

2 Answers 2

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

3
  • I've posted my view above. I granted the perm through Admin interface. The user is part of the group "Premium" .
    – picomon
    Jun 6, 2013 at 18:27
  • 1
    'paid' is supposed to be your application name. What is your app name?
    – Sid
    Jun 6, 2013 at 19:04
  • artapp is the appname . Paid is the model.
    – picomon
    Jun 6, 2013 at 19:40
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?

3
  • I should add that List is the class here. Jun 6, 2013 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 6, 2013 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. Jun 6, 2013 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.