Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:01:16+00:00 2026-05-17T01:01:16+00:00

I’m trying, in vain, to create a simple Django template tag to either show

  • 0

I’m trying, in vain, to create a simple Django template tag to either show or hide a “delete” link next to a submitted comment on my site.

In a nutshell, I want to pass the comment object to the template tag, determine if the currently logged in user is authorized to delete the comment and then either show or not show the link.

The usage in my template would be like so:

{% load access_tags %}
{% if_authorized comment %}
   <a href="{% url delete_comment comment.id %}">Delete</a>
{% endif_authorized %}

Rest assured that I also check in the appropriate view if the user is authorized to delete the comment.

Does this type of tag have a specific name? It would certainly help me with my Google searches if it did. Thanks for your help!

UPDATE 1:

The way my site works, two people are potentially authorized to delete a comment: 1) the comment creator and 2) the owner of the post where the comment was left. Because of this, I need to determine, per comment, if one of those conditions is present.

I don’t think I can use something like Django’s built-in permission sytem, since it requires that permissions “be set globally per type of object, no per specific object instance”.

In my case, user “Bob” may have permissions to delete a comment (if he wrote it or it is on a post he created), but he also may not be allowed to delete it (if he is looking at a comment on someone else’s post).

UPDATE 2:

It appears that you can’t pass objects to a template tag, only strings: “Although you can pass any number of arguments to a template tag using token.split_contents(), the arguments are all unpacked as string literals.” I guess I’ll pass the id of the comment object in question and pull it in the tag.

I was wrong about this, just have to access the passed in object like:

self.comment.resolve(context).user 

vs.

self.comment.user
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:01:17+00:00Added an answer on May 17, 2026 at 1:01 am

    OK, this is how I did it…

    The tag is used like this in the template:

       {% load access_tags %}
       {% if_authorized comment.user object.user user %}
          <a href="{% url delete_comment comment.id %}">Delete</a>
       {% endif_authorized %}
    

    The template tag file is called “access_tag.py” and is in my app’s “templatetags” directory. This is the contents of “access_tag.py”:

    from django.template import Node, NodeList, TemplateSyntaxError
    from django.template import Library, Variable, VariableDoesNotExist
    
    register = Library()
    
    def do_if_authorized(parser, token):
        """
        Outputs the contents of the block if the 'comment owner' or the 
        'page owner' is also the 'authenticated user'. As well, you can use
        an {% else %} tag to show text if the match fails.
    
        Takes three parameters:
          1) the comment owner
          2) the page owner
          3) the current authenticated user
        """
        bits = token.contents.split()
        if len(bits) != 4:
            raise TemplateSyntaxError("%s tag takes three arguments: \
                                       1) the comment owner \
                                       2) the page owner \
                                       3) the current authenticated user" % bits[0])
        nodelist_true = parser.parse(('else', 'endif_authorized'))
        token = parser.next_token()
    
        if token.contents == 'else':
            nodelist_false = parser.parse(('endif_authorized',))
            parser.delete_first_token()
        else:
            nodelist_false = NodeList()
        return IfAuthorizedNode(bits[1], bits[2], bits[3], nodelist_true, nodelist_false)
    
    class IfAuthorizedNode(Node):
        def __init__(self, comment_owner, page_owner, authenticated_user, nodelist_true, nodelist_false):
            self.nodelist_true = nodelist_true
            self.nodelist_false = nodelist_false
            self.comment_owner = Variable(comment_owner)
            self.page_owner = Variable(page_owner)
            self.authenticated_user = Variable(authenticated_user)
    
        def render(self, context):
            try:
                comment_owner = self.comment_owner.resolve(context)
                page_owner = self.page_owner.resolve(context)
                authenticated_user = self.authenticated_user.resolve(context)
            except VariableDoesNotExist:
                return ''
    
            if comment_owner == authenticated_user or page_owner == authenticated_user:
                return self.nodelist_true.render(context)
            else:
                return self.nodelist_false.render(context)
    
    register.tag('if_authorized', do_if_authorized)
    

    Done. In the end, it would have been pretty easy to just use the built-in {% if %} tag to do this comparison, but since I’ll have other per-object authorizations to do, I will continue to build out these custom “access_tags”. Plus, the template code looks so much tidier 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.