我在Django中创建了几个基于日期的视图,虽然年和月的视图功能与预期相同,但今天显示的视图未被检测到。例如,如果我尝试获取http://127.0.0.1:8000/blog/archive/2021/jan或http://127.0.0.1:8000/blog/archive/2021/jan/07/,视图将显示,http://127.0.0.1:8000/blog/archive/today/将失败。我知道问题肯定出在urls.py配置上,但我在文档中找不到它。
url.py
from django.urls import path, re_path
from blog import views
app_name = 'blog'
urlpatterns = [
# Example: /blog/
path('', views.PostListView.as_view(), name='index'),
# Example: /blog/post/ (same as /blog/)
path('post/', views.PostListView.as_view(), name='post_list'),
# Example: /blog/post/django-example/
re_path(r'^post/(?P<slug>[-\w]+)/$', views.PostDetailView.as_view(), name='post_detail'),
# Example: /blog/archive/
path('archive/', views.PostArchiveView.as_view(), name='post_archive'),
# Example: /blog/archive/2019/
path('archive/<int:year>/', views.PostYearArchiveView.as_view(), name='post_year_archive'),
# Example: /blog/archive/2019/nov/
path('archive/<int:year>/<str:month>/', views.PostMonthArchiveView.as_view(month_format = '%b'), name='post_month_archive'),
# Example: /blog/archive/2019/nov/10/
path('archive/<int:year>/<str:month>/<int:day>/', views.PostDayArchiveView.as_view(), name='post_day_archive'),
# Example: /blog/archive/today/
path('archive/today/', views.PostTodayArchiveView.as_view(), name='post_today_archive'),
]
views.py
from django.shortcuts import render
# Create your views here.
from django.views.generic import ListView, DetailView, ArchiveIndexView, YearArchiveView, MonthArchiveView, \
DayArchiveView, TodayArchiveView
from blog.models import Post
class PostListView(ListView):
model = Post
template_name = 'blog/post_all.html'
context_object_name = 'posts'
paginate_by = 2
class PostDetailView(DetailView):
model = Post
class PostArchiveView(ArchiveIndexView):
model = Post
date_field = 'modify_dt'
class PostYearArchiveView(YearArchiveView):
model = Post
date_field = 'modify_dt'
make_object_list = True
class PostMonthArchiveView(MonthArchiveView):
model = Post
date_field = 'modify_dt'
class PostDayArchiveView(DayArchiveView):
model = Post
date_field = 'modify_dt'
day_format = '%d'
class PostTodayArchiveView(TodayArchiveView):
model = Post
date_field = 'modify_dt'
models.py
from django.db import models
# Create your models here.
from django.urls import reverse
class Post(models.Model):
title = models.CharField(verbose_name='TITLE', max_length=50)
slug = models.SlugField('SLUG', unique=True, allow_unicode=True, help_text='one word for title alias.')
description = models.CharField('DESCRIPTION', max_length=100, blank=True, help_text='simple description text.')
content = models.TextField('CONTENT')
create_dt = models.DateTimeField('CREATE DATE', auto_now_add=True)
modify_dt = models.DateTimeField('MODIFY DATE', auto_now=True)
class Meta:
verbose_name = 'post'
verbose_name_plural = 'posts'
db_table = 'blog_posts'
ordering = ('-modify_dt',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail', args=(self.slug,))
def get_previous(self):
return self.get_previous_by_modify_dt()
def get_next(self):
return self.get_next_by_modify_dt()
错误
1条答案
按热度按时间eni9jsuy1#
根据https://docs.djangoproject.com/en/4.2/ref/class-based-views/generic-date-based/#todayarchiveview
您的类PostTodayArchiveView应如下所示