具有通用视图的 Django 年/月存档页面

发布于 2024-10-17 21:07:26 字数 799 浏览 8 评论 0原文

希望按月和年制作通用视图存档页面。 像这样:

2011 - January March
2010 - October December

我得到什么:

2011 - January January
2010 - January January

这可能吗?这是视图和模板。

看法

def track_archive(request):
    return date_based.archive_index(
        request,
        date_field='date',
        queryset=Track.objects.all(),
  )
track_archive.__doc__ = date_based.archive_index.__doc__

template
{% for year in date_list %}
        <a href="{% url track_archive %}{{ year|date:"Y" }}/">{{ year|date:"Y" }}</a> archives:
        {% for month in date_list %}
            <a href="{% url track_archive %}{{ year|date:"Y" }}/{{ month|date:"b" }}/">{{ month|date:"F" }}</a>
        {% endfor %}
    {% endfor %}

Looking to make a generic view archive page by month and year.
Like this:

2011 - January March
2010 - October December

What I am getting:

2011 - January January
2010 - January January

Is this possible? Here are the views and templates.

view

def track_archive(request):
    return date_based.archive_index(
        request,
        date_field='date',
        queryset=Track.objects.all(),
  )
track_archive.__doc__ = date_based.archive_index.__doc__

template
{% for year in date_list %}
        <a href="{% url track_archive %}{{ year|date:"Y" }}/">{{ year|date:"Y" }}</a> archives:
        {% for month in date_list %}
            <a href="{% url track_archive %}{{ year|date:"Y" }}/{{ month|date:"b" }}/">{{ month|date:"F" }}</a>
        {% endfor %}
    {% endfor %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千笙结 2024-10-24 21:07:26

根据 doc archive_index仅计算年份。您可能想要编写年/月分组:

def track_archive(request):
   tracks = Track.objects.all()
   archive = {}

   date_field = 'date'

   years = tracks.dates(date_field, 'year')[::-1]
   for date_year in years:
       months = tracks.filter(date__year=date_year.year).dates(date_field, 'month')
       archive[date_year] = months

   archive = sorted(archive.items(), reverse=True)

   return date_based.archive_index(
        request,
        date_field=date_field,
        queryset=tracks,
        extra_context={'archive': archive},
   )

您的模板:

{% for y, months in archive %}
<div>
  {{ y.year }} archives: 
  {% for m in months %}
    {{ m|date:"F" }}
  {% endfor %}
</div>
{% endfor %}

y 和 m 是日期对象,您应该能够提取任何日期格式信息来构建您的网址。

According to the doc, archive_index only calculates the years. You might want to write the year/month grouping:

def track_archive(request):
   tracks = Track.objects.all()
   archive = {}

   date_field = 'date'

   years = tracks.dates(date_field, 'year')[::-1]
   for date_year in years:
       months = tracks.filter(date__year=date_year.year).dates(date_field, 'month')
       archive[date_year] = months

   archive = sorted(archive.items(), reverse=True)

   return date_based.archive_index(
        request,
        date_field=date_field,
        queryset=tracks,
        extra_context={'archive': archive},
   )

Your template:

{% for y, months in archive %}
<div>
  {{ y.year }} archives: 
  {% for m in months %}
    {{ m|date:"F" }}
  {% endfor %}
</div>
{% endfor %}

y and m are date objects, you should be able to extract any date format information to construct your urls.

肥爪爪 2024-10-24 21:07:26

您可以这样做并坚持使用通用视图 - 如果您使用基于类的通用视图。

而不是使用 ArchiveIndexView
使用类似的内容

class IndexView(ArchiveIndexView):
    template_name="index.html"
    model = Article
    date_field="created"

    def get_context_data(self, **kwargs):
        context = super(IndexView,self).get_context_data(**kwargs)
        months = Article.objects.dates('created','month')[::-1]

        context['months'] = months
        return context

然后在模板中,您将获得月份字典,您可以按年份对其进行分组::

 <ul>
    {% for year, months in years.items %}
     <li> <a href ="{% url archive_year year %}"> {{ year }} <ul>
        {% for month in months %}
            <li> <a href ="{% url archive_month year month.month %}/">{{ month|date:"M Y" }}</a> </li>
        {% endfor %}
        </ul>
     </li>
    {% endfor %}
 </ul>

You can do it and stick with generic views - if you use class based generic views.

Instead of using ArchiveIndexView
use something like

class IndexView(ArchiveIndexView):
    template_name="index.html"
    model = Article
    date_field="created"

    def get_context_data(self, **kwargs):
        context = super(IndexView,self).get_context_data(**kwargs)
        months = Article.objects.dates('created','month')[::-1]

        context['months'] = months
        return context

Then in your template, you get the months dictionary, which you can group by year::

 <ul>
    {% for year, months in years.items %}
     <li> <a href ="{% url archive_year year %}"> {{ year }} <ul>
        {% for month in months %}
            <li> <a href ="{% url archive_month year month.month %}/">{{ month|date:"M Y" }}</a> </li>
        {% endfor %}
        </ul>
     </li>
    {% endfor %}
 </ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文