【问题标题】:Django 1.1 date-based generic view problem - archive_year, archive_month, archive_dayDjango 1.1 基于日期的通用视图问题——archive_year、archive_month、archive_day
【发布时间】:2010-12-28 06:01:59
【问题描述】:

我在我的第一个 Django 博客上,当我尝试使用 Django 的内置通用视图按年、月和日获取帖子时,但我没有得到正确的结果。 (对不起,我的第一个问题是非专业的。如果有人知道什么是合适的问题,请告诉我)

嗯,我觉得还是把我的配置给你看看,让自己更好看:

完成博客 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 = 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>

假设我有以下博客条目:

example.com/blog/2009/dec/18/test

现在请求

example.com/blog/2009/

我没有收到任何对象,但在给出完整 URL 时会显示帖子。

我认为 Django 在某个地方默默地失败了,虽然它处于调试模式,但我不知道在哪里。 我将不胜感激。

【问题讨论】:

    标签: python html django django-templates


    【解决方案1】:

    月份信息存储在上下文变量date_list,而不是pub_date

    来自archive_year 的 django 文档:

    模板上下文:

    除了extra_context, 模板的上下文将是:

    • date_list:代表所有的 datetime.date 对象列表 有可用对象的月份 给定的年份,根据查询集, 按升序排列。

    以下应该可以解决问题:

    {% for month in date_list %}
        <li>
            <a href="/blog/{{ year }}/{{ month|date:"b" }}/">{{ month|date:"F" }}</a>
        </li>
    {% endfor %}
    

    【讨论】:

    • 就是这样.. 我错过了pub_date 的重点。谢谢阿拉斯代尔
    猜你喜欢
    • 2012-06-09
    • 2011-04-24
    • 2011-08-06
    • 2011-09-09
    • 2012-01-28
    • 2013-04-02
    • 2010-10-14
    • 2011-09-19
    • 1970-01-01
    相关资源
    最近更新 更多