user823148
user823148

Reputation: 147

Python Django template

Can I catch in any way the current year in the Django template language? I need to create a link for the current year and I don't want to pass it by hand. And sending it via the view throws me an error:

Caught NoReverseMatch while rendering: Reverse for 'blog_archive_year' with arguments '()' and keyword arguments '{'year': ''}' not found.

Here is the url:

url(r'^(?P<year>\d{4})/$',
        view='post_archive_year',
        name='blog_archive_year'

view

def post_archive_year(request, year, **kwargs):
    #now = datetime.datetime.now()
    #current_year = now.year
    return date_based.archive_year(
        request,
        #year=current_year,
        year=year,
        date_field='publish',
        queryset=Post.objects.published(),
        make_object_list=True,
        **kwargs
    )
post_archive_year.__doc__ = date_based.archive_year.__doc__

and this is the dummy template: <

<a href="{% url blog_archive_year year=2011(I'd like to send him year here) %}">Articols archive</a>

Please help:)

Upvotes: 1

Views: 199

Answers (1)

Reto Aebersold
Reto Aebersold

Reputation: 16624

First you should define the year pattern in the url:

url(r'^(?P<year>\d{4})/$', 'post_archive_year', name='blog_archive_year')

To print the current year in a template use:

{% now Y %}

Upvotes: 2

Related Questions