Custom Django Template Template with Context Variable Argument

I have my own template tag that shows a calendar. I want to fill in certain elements in a calendar based on a dynamic value.

Here is the tag:

@register.inclusion_tag("website/_calendar.html") def calendar_table(post): post=int(post) imp=IMP.objects.filter(post__pk=post) if imp: ...do stuff 

In my template, it works fine when I pass a hard-coded value like

  {% load inclusion_tags %} {% calendar_table "6" %} 

However, when I try something like {% calendar_table "{{post.id}}"%}, it throws a ValueError error for an int () attempt. How can I get around this?

+6
source share
1 answer

Do you want {% calendar_table post.id %} ; extra {{ and }} is what causes you to have heartburn.

Note that in your custom tag, you need to take a string ( "post.id" ) that will be passed and resolve it in context using Variable.resolve . There is more information about this in the Django docs; in particular, see here: http://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#passing-template-variables-to-the-tag

+9
source

Source: https://habr.com/ru/post/888365/


All Articles