'Could not parse the remainder: '(audio_only=True)'
in my templates/videos.html,
<div class="grid grid-cols-3 gap-2">
{% for vid in videos.streams.filter(audio_only=True) %}
<a href="{{vid.url}}">{{vid.resolution}}</a>
{% endfor %}
</div>
Error is,
Could not parse the remainder: '(audio_only=True)' from 'videos.streams.filter(audio_only=True)'
I can solve this when i pass all_videos = videos.streams.filter(audio_only=True) from my views.py as context, and in templates/videos.html i replace videos.streams.filter(audio_only=True) with all_videos,
but I want to know that is there any other method to solve this
Solution 1:[1]
but I want to know that is there any other method to solve this.
You could add an extra property/method with no parameters to the Video model and call that property to obtain the filtered queryset.
For example:
class Video(models.Model):
# …
@property
def audio_streams(self):
return self.streams.filter(audio_only=True)
and then thus use {% for video in videos.audio_streams %}. But regardless, the Django template language has been deliberately restricted to prevent people from writing this: a template should implement rendering logic, not business logic: business logic belongs in the models and the views. So the only clean solution is to filter in the view, not the template.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
