【发布时间】:2019-01-21 10:12:24
【问题描述】:
我尝试访问的网址是 blog/2018/08 和 blog/2018/08/14。 其他网址,例如 blog/、blog/2018、blog/today、archive/ 效果很好 但包含月份和日期的网址不起作用。
这是我的代码:
博客/urls.py
from django.urls import path
from blog.views import *
app_name = 'blog'
urlpatterns = [
path('', PostLV.as_view(), name='index'),
path('post/', PostLV.as_view(), name='post_list'),
path('post/<slug:slug>/', PostDV.as_view(), name='post_detail'),
path('archieve/', PostAV.as_view(), name='post_archive'),
path('<int:year>/', PostYAV.as_view(), name='post_year_archive'),
path('<int:year>/<int:month>/', PostMAV.as_view(), name='post_month_archive'),
path('<int:year>/<int:month>/<int:day>/', PostDAV.as_view(), name='post_day_archive'),
path('today/', PostTAV.as_view(), name='post_today_archive'),
]
blog/views.py
from django.views.generic import ListView, DetailView
from django.views.generic.dates import ArchiveIndexView, YearArchiveView, MonthArchiveView, DayArchiveView, TodayArchiveView
from blog.models import Post
# Create your views here.
class PostLV(ListView):
model = Post
template_name = 'blog/post_all.html'
context_object_name = 'posts'
paginate_by = 2
class PostDV(DetailView):
model = Post
class PostAV(ArchiveIndexView):
model = Post
date_field = 'modify_date'
template_name = 'blog/post_archive.html'
class PostYAV(YearArchiveView):
model = Post
date_field = 'modify_date'
make_object_list = True
template_name = 'blog/post_archive_year.html'
class PostMAV(MonthArchiveView):
model = Post
date_field = 'modify_date'
template_name = 'blog/post_archive_month.html'
class PostDAV(DayArchiveView):
model = Post
date_field = 'modify_date'
template_name = 'blog/post_archive_day.html'
class PostTAV(TodayArchiveView):
model = Post
date_field = 'modify_date'
template_name = 'blog/post_archive_today.html'
views.py 中提到了我得到的所有模板!
非常感谢你
【问题讨论】:
-
当您尝试访问这些 URL 时究竟会发生什么?
-
可能,您没有在它们的末尾附加斜杠。只是猜测。
-
您收到 404 或“TemplateDoesNotExist”?还是什么?
-
@DanielRoseman 天哪,我忘记写错误了...我正在获取找不到页面 (404) 请求方法:GET 请求 URL:127.0.0.1:8000/blog/2018/08 引发者:blog.views.PostMAV 无效的日期字符串' 2018__8__' 给定格式 '%Y__%b__'
标签: django django-models django-templates django-views