Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if..else custom template tag

I'm implementing a custom permissions application in my Django project, and I'm lost as to how to implement a custom template tag that checks a logged in user's permissions for a specific object instance and shows a piece of HTML based on the outcome of the check.

What I have now is (pseudocode):

{% check_permission request.user "can_edit" on article %}
    <form>...</form>
{% endcheck %}

('check_permission' is my custom template tag).

The templatetag takes in the user, the permission and the object instance and returns the enclosed HTML (the form). This currently works fine.

What I would like to do however, is something like:

{% if check_permission request.user "can_edit" on article %}
    <form>...</form>
{% else %}
    {{ article }}
{% endif %}

I've read about the assignment tag, but my fear is that I would pollute the context variable space with this (meaning I might overwrite previous permission context variables). In other words, as the context variables are being defined on different levels (the view, middleware in my case, and now this assignment template tag), I'm worried about maintainability.

like image 462
LaundroMat Avatar asked Oct 17 '11 09:10

LaundroMat


People also ask

How do I create a custom template tag?

Create a custom template tagUnder the application directory, create the templatetags package (it should contain the __init__.py file). For example, Django/DjangoApp/templatetags. In the templatetags package, create a . py file, for example my_custom_tags, and add some code to it to declare a custom tag.

How do I use template tags?

The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads. The content inside <template> can be rendered later with a JavaScript. You can use the <template> tag if you have some HTML code you want to use over and over again, but not until you ask for it.

Which tag is used for template inheritance?

extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.


2 Answers

inside my_tags.py

from django import template
register = template.Library()

@register.simple_tag(takes_context=True)
def make_my_variable_true(context):
    context['my_variable'] = True
    return ''  # without this you'll get a "None" in your html

inside my_template.html

{% load my_tags %}
{% make_my_variable_true %}
{% if my_variable %}foo{% endif %}
like image 111
mathandy Avatar answered Oct 01 '22 02:10

mathandy


You can use template filters inside if statements. So you could rewrite your tag as a filter:

{% if request.user|check_can_edit:article %}

Note that it's tricky to pass multiple arguments of different types to a filter, so you'll probably want to use one filter per permission, above I've used check_can_edit.

like image 24
Daniel Roseman Avatar answered Oct 01 '22 02:10

Daniel Roseman