SlideShare a Scribd company logo
1 of 12
Django 
user permissions 
in your templates 
A nice little Django template tag pattern
The task 
On the frontend 
display an “edit” link 
for the owner of an object and 
for super users.
Version A 
{% if user.is_authenticated %} 
{% if user.is_superuser %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% else %} 
{% if my_obj.user == user %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% endif %} 
{% endif %} 
{% endif %}
Version A 
{% if user.is_authenticated %} 
{% if user.is_superuser %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% else %} 
{% if my_obj.user == user %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% endif %} 
{% endif %} 
{% endif %}
Version B 
{% if user.is_authenticated and my_obj.user == user or 
user.is_superuser %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% endif %}
Version B 
{% if user.is_authenticated and my_obj.user == user or 
user.is_superuser %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% endif %}
Version C 
{% if user|can_edit:my_obj %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% endif %}
Version C 
{% if user|can_edit:my_obj %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% endif %} 
Way better!
Version C: 
{% if user|can_edit:my_obj %} 
<a href="{% url 'my_obj:edit' my_obj.id %}"> 
Edit object 
</a> 
{% endif %}
The custom template tag 
from django import template 
register = template.Library() 
@register.filter 
def can_edit(user, obj): 
user_can_edit = False 
if user.is_authenticated: 
if user.is_superuser: 
user_can_edit = True 
else: 
if obj and obj.user and obj.user == user: 
user_can_edit = True 
return user_can_edit
More applications for this pattern 
{% if user|can_delete:my_object %} 
{% if user|is_in_group:group %} 
{% if event|is_attended_by:user %} 
{% if user|has_been_at:place %} 
{% if place|is_in_favorites_of:user %} 
{% if article|has_been_flagged_by:user %}
Thank you for listening! 
Anton Pirker 
anton@ignaz.at 
@antonpirker 
Slides 
slideshare.net/apirker 
Blog post 
http://www.anton-pirker.at/django-user-permissions-in-your- 
templates/

More Related Content

What's hot

Introduction of pg_statsinfo and pg_stats_reporter ~Statistics Reporting Tool...
Introduction of pg_statsinfo and pg_stats_reporter ~Statistics Reporting Tool...Introduction of pg_statsinfo and pg_stats_reporter ~Statistics Reporting Tool...
Introduction of pg_statsinfo and pg_stats_reporter ~Statistics Reporting Tool...Kondo Mitsumasa
 
Budget upload process in SAP PS
Budget upload process in SAP PSBudget upload process in SAP PS
Budget upload process in SAP PSgovil79
 
SAP FICO Online Training
SAP FICO Online Training SAP FICO Online Training
SAP FICO Online Training Thanusha154
 
レコード計算の多相型システムと型推論
レコード計算の多相型システムと型推論レコード計算の多相型システムと型推論
レコード計算の多相型システムと型推論T. Suwa
 
Sap s4 hana 1709 what's new in financial planning & analysis
Sap s4 hana 1709   what's new in financial planning & analysisSap s4 hana 1709   what's new in financial planning & analysis
Sap s4 hana 1709 what's new in financial planning & analysisEdwin Weijers
 
Sap transaction-codes-list
Sap transaction-codes-listSap transaction-codes-list
Sap transaction-codes-listRaj p
 
ABAP Open SQL & Internal Table
ABAP Open SQL & Internal TableABAP Open SQL & Internal Table
ABAP Open SQL & Internal Tablesapdocs. info
 
Controlling 2012 Global Rollouts.pptx
Controlling 2012 Global Rollouts.pptxControlling 2012 Global Rollouts.pptx
Controlling 2012 Global Rollouts.pptxJohn Jordan
 
SAP S4 HANA FI 1610 Overview (mindmap edition)
SAP S4 HANA FI 1610 Overview (mindmap edition)SAP S4 HANA FI 1610 Overview (mindmap edition)
SAP S4 HANA FI 1610 Overview (mindmap edition)Benedict Yong (杨腾翔)
 
Sap pm tables
Sap pm tablesSap pm tables
Sap pm tablesvbpc
 
ABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infoABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infosapdocs. info
 
Sap script made easy
Sap script made easySap script made easy
Sap script made easyKranthi Kumar
 

What's hot (20)

Introduction of pg_statsinfo and pg_stats_reporter ~Statistics Reporting Tool...
Introduction of pg_statsinfo and pg_stats_reporter ~Statistics Reporting Tool...Introduction of pg_statsinfo and pg_stats_reporter ~Statistics Reporting Tool...
Introduction of pg_statsinfo and pg_stats_reporter ~Statistics Reporting Tool...
 
Budget upload process in SAP PS
Budget upload process in SAP PSBudget upload process in SAP PS
Budget upload process in SAP PS
 
SAP FICO Online Training
SAP FICO Online Training SAP FICO Online Training
SAP FICO Online Training
 
SAP PPM 5.0 Trainings
SAP PPM 5.0 TrainingsSAP PPM 5.0 Trainings
SAP PPM 5.0 Trainings
 
Abap dictionary 1
Abap dictionary 1Abap dictionary 1
Abap dictionary 1
 
レコード計算の多相型システムと型推論
レコード計算の多相型システムと型推論レコード計算の多相型システムと型推論
レコード計算の多相型システムと型推論
 
Sap s4 hana 1709 what's new in financial planning & analysis
Sap s4 hana 1709   what's new in financial planning & analysisSap s4 hana 1709   what's new in financial planning & analysis
Sap s4 hana 1709 what's new in financial planning & analysis
 
BRF+ Walk through
BRF+ Walk throughBRF+ Walk through
BRF+ Walk through
 
Sap transaction-codes-list
Sap transaction-codes-listSap transaction-codes-list
Sap transaction-codes-list
 
ABAP Open SQL & Internal Table
ABAP Open SQL & Internal TableABAP Open SQL & Internal Table
ABAP Open SQL & Internal Table
 
Sap pm test scripts
Sap pm test scriptsSap pm test scripts
Sap pm test scripts
 
SAP Connector.
SAP Connector.SAP Connector.
SAP Connector.
 
Dip profiles-documentation
Dip profiles-documentationDip profiles-documentation
Dip profiles-documentation
 
Controlling 2012 Global Rollouts.pptx
Controlling 2012 Global Rollouts.pptxControlling 2012 Global Rollouts.pptx
Controlling 2012 Global Rollouts.pptx
 
SAP S4 HANA FI 1610 Overview (mindmap edition)
SAP S4 HANA FI 1610 Overview (mindmap edition)SAP S4 HANA FI 1610 Overview (mindmap edition)
SAP S4 HANA FI 1610 Overview (mindmap edition)
 
SAP workflow events
SAP workflow eventsSAP workflow events
SAP workflow events
 
Sap pm tables
Sap pm tablesSap pm tables
Sap pm tables
 
sap script overview
sap script overviewsap script overview
sap script overview
 
ABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infoABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.info
 
Sap script made easy
Sap script made easySap script made easy
Sap script made easy
 

Django user permissions in your templates

  • 1. Django user permissions in your templates A nice little Django template tag pattern
  • 2. The task On the frontend display an “edit” link for the owner of an object and for super users.
  • 3. Version A {% if user.is_authenticated %} {% if user.is_superuser %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% else %} {% if my_obj.user == user %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% endif %} {% endif %} {% endif %}
  • 4. Version A {% if user.is_authenticated %} {% if user.is_superuser %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% else %} {% if my_obj.user == user %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% endif %} {% endif %} {% endif %}
  • 5. Version B {% if user.is_authenticated and my_obj.user == user or user.is_superuser %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% endif %}
  • 6. Version B {% if user.is_authenticated and my_obj.user == user or user.is_superuser %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% endif %}
  • 7. Version C {% if user|can_edit:my_obj %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% endif %}
  • 8. Version C {% if user|can_edit:my_obj %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% endif %} Way better!
  • 9. Version C: {% if user|can_edit:my_obj %} <a href="{% url 'my_obj:edit' my_obj.id %}"> Edit object </a> {% endif %}
  • 10. The custom template tag from django import template register = template.Library() @register.filter def can_edit(user, obj): user_can_edit = False if user.is_authenticated: if user.is_superuser: user_can_edit = True else: if obj and obj.user and obj.user == user: user_can_edit = True return user_can_edit
  • 11. More applications for this pattern {% if user|can_delete:my_object %} {% if user|is_in_group:group %} {% if event|is_attended_by:user %} {% if user|has_been_at:place %} {% if place|is_in_favorites_of:user %} {% if article|has_been_flagged_by:user %}
  • 12. Thank you for listening! Anton Pirker anton@ignaz.at @antonpirker Slides slideshare.net/apirker Blog post http://www.anton-pirker.at/django-user-permissions-in-your- templates/