[Fixed]-Pass Django Model Object into Template Tag

1👍

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" %}

Leave a comment