/blog/2018/ 处的 NoReverseMatch

NoReverseMatch at /blog/2018/

请帮帮我。我不知道这个问题...
有什么问题吗?
Html代码图片:

错误页面图像:

blog/urls.py

urlpatterns = [
      -- skip --

      # Example: /2018/nov/
      url(r'^(?P<year>\d{4})/$', PostYAV.as_view(), name='post_year_archive'),

      -- skip --
]

blog/views.py

from blog.models import Post
from django.views.generic.dates import --skip--, YearArchiveView, --skip--

-- skip --

class PostYAV(YearArchiveView):
     model = Post
     date_field = 'modify_date'
     make_object_list = True

如果你正在尝试这个 url:

 url(r'^(?P<year>\d{4})/$', PostYAV.as_view(), name='post_year_archive'),

然后像这样从模板调用它:

{% url 'blog:post_year_archive' year|date:'Y' %}

视图应该是这样的:

class PostYAV(YearArchiveView):
    model = Post
    date_field = 'modify_date'
    make_object_list = True

    def get(self, request, year, *args, **kwargs):
        post = Post.objects.filter(created__year=year)  # assuming created= models.DateField() or similar

但是如果你也有一个月(就像与问题分享的图片一样,那么试试这个)

url(r'^(?P<year>\d{4})/(?P<month>\d{2})/$', PostYAV.as_view(), name='post_year_archive'),

模板:

  {% url 'blog:post_year_archive' year|date:'Y' month|date:'m' %}

查看:

class PostYAV(YearArchiveView):
    model = Post
    date_field = 'modify_date'
    make_object_list = True

    def get(self, request, year, month, *args, **kwargs):
        post = Post.objects.filter(created__year=year, created__month=month)  # assuming created= models.DateField() or similar

您问题中的错误发生是因为 url 正则表达式与从模板传递的参数不匹配。 %b 显示的是本地化的月份,与正则表达式 [a-z]{3} 不匹配。因此 %m 会将月份作为数字传递,我们已经更新了月份的正则表达式以捕获从模板发送的数字。