Arbitrary number of positional arguments in django include tag?

I am trying to write a django inclusion tag that takes an arbitrary number of arguments:

@register.inclusion_tag('so.html')
def table_field(*args):
fields = []
for arg in args:
    fields.append(arg)
return { 'fields': fields, }

However, when I call this from the django template engine:

{% table_field form.hr form.bp form.o2_sat %}

I get an error message:

table_field takes 0 arguments

Is this another limitation of the django template engine?

+3
source share
5 answers

This is another limitation of django inclusion tags. Currently this is not possible in the django trunk version.

0
source

Starting with 1.2.1, you can pass arguments to inclusion tags.

Here is an example from my mods in django vote templatetags

@register.inclusion_tag("voting/vote_form.html", takes_context=True)
def vote_form(context, vote_object, vote_dict, score_dict):
    if isinstance(vote_dict, dict):

and the template looks like this:

{% vote_form bookmark vote_dict score_dict %}

, , - , , .

, , take_context, , , .

* args , - , # , .

+3

Re: django,

, , .

, , :

@register.inclusion_tag('so.html')
def table_field(args):
    return { 'fields': [arg for arg in args], }

:

{% table_field whatever_was_passed_in_from_the_view %}

, , .

+2
source

You will have to write your own template tag, which I assume.

+1
source

The current development version provides a variable number of arguments for the inclusion tag. The patch is described here:

https://code.djangoproject.com/ticket/13956

It will be released with 1.4, see release notes .

+1
source

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


All Articles