0
This is another limitation of django’s inclusion tags. Currently, in django’s trunk version, this cannot be done.
3
As of 1.2.1 you can pass arguments to inclusion tags.
Here is an example from my mods to django voting 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:
{% vote_form bookmark vote_dict score_dict %}
What I don’t like about this is there is no way to name the arguments, only to put them in order, but it does work.
What is not so clear to me right now is why when you specify takes_context, that parent context is not passed along with the context that you return to be used in rendering the inclusion template.
Your trying to use *args won’t work though because the # of args passed is checked against the function.
- [Django]-Changing models in django results in broken database?
- [Django]-Django1.4.0:ImportError: No module named base
2
Re: I’m trying to write a django inclusion tag that takes an arbitrary number of arguments
I think you should pass the arbitrary number of arguments in from the view as a single argument, as some sort of collection.
Based on the documentation, I think you should pass some collection object from the view to the template to the inclusion tag, and write the inclusion tag as:
@register.inclusion_tag('so.html')
def table_field(args):
return { 'fields': [arg for arg in args], }
Then your template looks like this:
{% table_field whatever_was_passed_in_from_the_view %}
I don’t think that templates are expected to make presentation decisions regarding the selection of data and that that decision is best handled in the view.
- [Django]-Unable to understand a line from the django tutorial
- [Django]-Django settings based on IP or hostname
- [Django]-(Django and PIL) encoder error -2 when writing image file
- [Django]-Django DecimalField returns "None" instead of empty value
- [Django]-Django : Get user IP from development server
- [Django]-Using Django CreateView to handle formset – It doesnt validate
1
The current development version does provide 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.