I'm on my first Django blog and when trying to get the posts by year, month and day, using the built-in generic view from Django, but I don't get proper results. (Sorry for my non-professional first question.. if someone knows what is the appropriate question, please let me know)
Well, I think it's better to show you my configuration to make yourself a better picture:
Complete blog URLconf:
from django.conf.urls.defaults import *
from weblog.models import Entry
entry_info_dict = {
'queryset': Entry.published,
'date_field': 'pub_date',
'template_object_name': 'Entry',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^$', 'archive_index', entry_info_dict, 'weblog_entry_archive_index'),
(r'^(?P<year>\d{4})/$',
'archive_year', entry_info_dict,
'weblog_entry_archive_year'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/$',
'archive_month',
entry_info_dict,
'weblog_entry_archive_month'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
'archive_day',
entry_info_dict,
'weblog_entry_archive_day'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
'object_detail',
entry_info_dict,
'weblog_entry_detail'),
)
urls.py:
urlpatterns开发者_如何学Python = patterns('',
(r'^blog/', include('weblog.urls.entries')),
...
)
entry_archive_year.html:
<h2>Archive for {{ year }}</h2>
<ul>
{% for month in pub_date %}
<li>
<a href="/blog/{{ year }}/{{ month|date:"b" }}/">{{ month|date:"F" }}</a>
</li>
{% endfor %}
</ul>
Supposing I have the following blog entry:
example.com/blog/2009/dec/18/test
and now request
example.com/blog/2009/
I get no objects, though when giving the full URL the post is shown.
I think Django is failing silently somewhere, though it's in DEBUG mode, and I can't figure out where. I'd appreciate any support with this one.
The month information is stored in the context variable date_list
, not pub_date
.
From the django docs for archive_year
:
Template context:
In addition to
extra_context
, the template's context will be:
date_list
: A list of datetime.date objects representing all months that have objects available in the given year, according to queryset, in ascending order.
The following should do the trick:
{% for month in date_list %}
<li>
<a href="/blog/{{ year }}/{{ month|date:"b" }}/">{{ month|date:"F" }}</a>
</li>
{% endfor %}
精彩评论