[Django]-Django template: How to use values/values_list

2๐Ÿ‘

โœ…

Can I pass argument like this in template?

No. The Django template engine deliberately restricts this, since this is logic that belongs in the view. For example:

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    following = request.user.profile.following.values_list('follow_to', flat=True)
    return render(
        request,
        'some_template.html',
        {'following': following}
    )

You can then render this with:

{{ following }}

That being said, using .values_list(โ€ฆ) [Django-doc] is often an anti-pattern, since it erodes the model layer. It is thus something related to the primitive obsession antipattern [refactoring.guru].

Leave a comment