【问题标题】:Url with MonthArchiveView give me error 404带有 MonthArchiveView 的网址给我错误 404
【发布时间】:2017-08-29 08:37:43
【问题描述】:

我想创建一个站点 (Django 1.8),我可以在其中按日期选择事件,确切地说是在给定月份和年份中发生的事情。 我决定使用通用类 MonthArchiveView。

主页的 url 传递年份和月份,url 也是如此 - 但我收到 404 错误

我的看法:

class BookingListView(ListView, MonthArchiveView):
    model = models.Booking
    queryset = models.Booking.objects.order_by('-date_start')
    paginate_by = 80
    template_name = 'events/archive_list.html'
    context_object_name = 'object_list'
    date_field = 'date_start'
    allow_future = True

    def get_context_data(self, **kwargs):
        context = super(BookingListView, self).get_context_data(**kwargs)
        context['mode'] = 'archive'
        return context

我的网址:

url(r'^(?P<year>[0-9]{4})/(?P<month>\d{2})/$', views.BookingListView.as_view(), name="list"),

我的硬编码参数链接):

<a href="{% url 'archive:list' 2017 09 %}" title="{% trans 'Archive' %}">
{% trans 'Archive' %}#}
</a>

【问题讨论】:

    标签: python django date url django-class-based-views


    【解决方案1】:

    如果您在该月没有任何预订,则月存档视图将返回 404。您可以通过将 allow_empty 设置为 True 来更改此行为。

    class BookingListView(ListView, MonthArchiveView):
        model = models.Booking
        allow_empty = True
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多