python - Cannot use filter inside Django template html -


i have issue on django project. have situation follows:

{% subobject in mainobject.subobjects.all %} 

this works nice, every subobject gets iterated nicely. want print subset of objects, like:

{% subobject in mainobject.subobjects.filter(somefield=somevalue) %} 

so far have searched solutions error get:

could not parse remainder: '(somefield=somevalue)' 

but did not find solution how line should different when filter used. want tweak template.html file, don't want make change on views.py file (where supposedly work nicely).

how achieve this?

following @yuji'tomira'tomita's comment..

don't put logic template, quote django docs:

philosophy

if have background in programming, or if you’re used languages mix programming code directly html, you’ll want bear in mind django template system not python embedded html. design: template system meant express presentation, not program logic.

better define queryset in view , pass template:

view:

def my_view(request):     ...     my_objects = mainobject.subobjects.filter(somefield=somevalue)     return render(request, 'mytemplate.html', {'my_objects': my_objects}) 

template:

{% subobject in my_objects %}     ... {% endfor %} 

hope helps.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -