user637965
user637965

Reputation:

generic views in django - how to make date variable output correct format

Here is the template for the monthly archive (of a blog). Note the {{ month }} variable here gives me the entire date for the first day of the month e.g. Aug. 1, 2011. How can I can make the variable output just the month (e.g. Aug)?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Monthly Archive</title>
    </head>
    <body>
        <h1>Monthly Archive - {{ month }}</h1>
        {% for entry in object_list %}
        <p><a href="{{ entry.get_absolute_url }}">{{ entry.title }}</a></p>
        {% endfor %}
    </body>
</html>

Here is the url page:

entry_info_dict = {
    'queryset': Entry.objects.all(),
    'date_field': 'pub_date',
}

    urlpatterns = patterns('',
        (r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', 'django.views.generic.date_based.object_detail', entry_info_dict),
        (r'^weblog/(?P<year>\d{4})/$',
          'django.views.generic.date_based.archive_year',
          entry_info_dict),
        (r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/$',
          'django.views.generic.date_based.archive_month',
          entry_info_dict),
        (r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
          'django.views.generic.date_based.archive_day',
          entry_info_dict),
        (r'^weblog/$', 'django.views.generic.date_based.archive_index',
          entry_info_dict),)

UPDATE

Okay, I figured out how to filter just the month. I have to use {{ month|date:"F" }} instead of just {{ month }}. However, I'm not familiar with the syntax here. Why is it "F"?

Upvotes: 0

Views: 185

Answers (1)

Rob Osborne
Rob Osborne

Reputation: 5005

Use the date filter has you updated your question to indicate. The 'F' is a historical choice established by PHP and followed by Python. They just ran out of letters.

Upvotes: 1

Related Questions