I have writing a template tag and take it to the templates:
{% check_somethings value1 value2 as is_checked %}
{% if is_checked %}
# do it
{% endif %}
But there are some errors. I am doing so right?
开发者_如何学运维check_somethings takes 2 arguments
There are:
@register.simple_tag
def check_somethings(value1, value2):
if Mymodel.objects.filter(f1=value1, f2=value2):
return True
else:
return False
The "as something" pattern is not built in to Django tags, your tag has to explicitly provide that functionality, which you can't do with simpletag. You'll have to write a full Node and parser function, which is harder than it ought to be; but you can look at examples of built-in tags.
Template tag parsing is very low level. You have passed your template tag four arguments: value1
, value2
, as
, and is_checked
. I'm not sure how to do what you want. I'd check the code of tags that already do it, and compare. I'm pretty sure @simpletag isn't going to cover it.
Take a look at the smart if tag. Apparently it will be built in to 1.2.
精彩评论