I have a string which needs to passed into a django template filter. But just the string alone, there are no django models involved, and I think this is what’s causing the issue.
var pk = "123";
var age = "{{ pk|pk_into_age }}";
In my django template filter code:
def pk_into_age(pk):
timestamp = Model.objects.get(pk=int(pk)).timestamp
return humanizeTimeDiff(timestamp) // Ex: "5 hours ago"
return timestamp
However this is not working, the error is that the “pk” value is just an empty string “”, which is not the case, it’s not grabbing the pk string literal from above.
Thanks fro helping
You are calling the template filter in a wrong way. Please see below:
You can not pass the javascript variable into the template filter, passed the variable from context.
Assuming
pkvariable exists in the context. Or hardcode the pk value as: