Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Pass Django Model Object into Template Tag

I created a custom template tag to query a list of objects, but each object has a tag associated with it. I would like to pass an object as a filter into my template tag to display only certain tagged objects in my template.

Template Tag

@register.inclusion_tag(
    'tags/_documents_snippets.html',
    takes_context=True
)
def document_snippets(context):
    Document = get_document_model()
    documents = Document.objects.all()
    return {
        'documents': documents,
        'request': context['request'],
    }

Template

<div class="col-md-12">
    <ul class="c-content-list-1 c-separator-dot c-square">
        {% for doc in documents %}
            <li><a href="{{ doc.url }}">{{ doc.title }}</a></li>
        {% endfor %}
    </ul>
</div>

Tag

{% document_snippets %}

Can I do something like {% document_snippets|tags="AO Now" %}

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can add arbitrary arguments to template tags like you can to regular functions. For example:

@register.inclusion_tag(
    'tags/_documents_snippets.html',
    takes_context=True
)
def document_snippets(context, **kwargs):
    Document = get_document_model()
    documents = Document.objects.filter(**kwargs)
    return {
        'documents': documents,
        'request': context['request'],
    }

And then call it with:

{% document_snippets tags="AO Now" %}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!