[Fixed]-How to supply a QuerySet to an included template?

1πŸ‘

βœ…

A context processor would be the typical way to do this.

def recent_issues(request):
    {'recent_issues': return Issue.objects.order_by('-issue_num')[1:6]}

Once you add the context processor to your TEMPLATES settings, you can access recent_issues in the template.

Alternatively, if you don’t want a context processor to run for every view, you can create a template tag using the simple_tag decorator (in Django < 1.9 use an assignment tag).

@register.simple_tag
def recent_issues():
    return Issue.objects.order_by('-issue_num')[1:6]

In your template, use the tag to assign the queryset to a variable

{% recent_issues as recent_issues %}

You can now loop through recent_issues in the template.

πŸ‘€Alasdair

0πŸ‘

Maybe I don’t understand your question very well, but are you looking for something like this:

{% include "some_include_template.html" with some_list=some_list some_var=some_var %}

?

πŸ‘€dentemm

Leave a comment