[Answer]-How do I set a template argument value to the value of another tag?

0πŸ‘

βœ…

It is not possible to include one tag inside another like this.

You can create an assignment tag, which stores the tag’s result in a context variable instead of outputting it.

The example assignment tag in the docs is for a template tag get_current_time, which you could use instead of the {% now %} tag.

In your mytags.py template tag module:

from django import template

register = template.Library()

@register.assignment_tag
def get_current_time(format_string):
    return datetime.datetime.now().strftime(format_string)

In your template:

{% load mytags %}
{% get_current_time "%Y" as year %}
<li><a href="{% url 'blog_archive' year=year %}">Archive</a></li>
πŸ‘€Alasdair

1πŸ‘

May be better decision is would be set default parameter at the urls.py ?

Example urls.py:

from django.utils.timezone import now
...
urlpatterns = patterns('app.views',
    url(r'^.../$', 'blog_archive', {'year': now().strftime('%Y')}),
)

0πŸ‘

That is what you want.

Maybe this:

<li><a href="{% url 'blog_archive' year=value|date:"Y"  %}">Archive</a></li>
# just pass the datetime object as 'value'
πŸ‘€Leandro

Leave a comment