请帮帮我。我不知道这个问题...
怎么了?
html代码图片:enter image description here
错误页面图片:enter image description here
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
答案 0 :(得分:0)
如果您尝试使用此网址:
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
会以月份作为数字传递,我们已经更新了月份的正则表达式以捕获模板发送的数字。