我在Django中创建了几个基于日期的视图,虽然年份和月份的视图与预期的一样,但显示天数的视图没有被检测到。例如,如果我试图获得http://127.0.0.1:8000/blog/archive/2021/,或者http://127.0.0.1:8000/blog/archive/2021/01视图将被显示,http://127.0.0.1:8000/blog/archive/2021/01/07将失败。我理解urls.py配置的问题,但我在文档中找不到它。我还尝试将day_format=‘’%d‘传递给as_view方法,但没有任何结果。
urls.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>/<int:month>/', views.PostMonthArchiveView.as_view(month_format='%m'), 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'
class PostTodayArchiveView(TodayArchiveView):
model = Post
date_field = 'modify_dt'
post_archive_day.html
<h1>Post Archives for {{ day|date:"N d, Y" }}</h1>
<div>
<ul>
{% for post in object_list %}
<li>
{{ post.modify_dt|date:"Y-m-d" }}
<a href="{{ post.get_absolute_url }}"><strong>{{ post.title }}</strong></a>
</li>
{% endfor %}
</ul>
</div>
误差
发布于 2021-01-09 05:15:57
您的URL模式是正确匹配的,特别是'archive/<int:year>/<str:month>/<int:day>/'
将与URL http://127.0.0.1:8000/blog/archive/2021/01/07匹配。
错误消息指示无效的日期字符串。它期望指定为%b
的月份字段是缩写的月份名称。因此,您的URL应该是http://127.0.0.1:8000/blog/archive/2021/jan/07,按照urls.py
中您的URL模式上面的注释。
更新
通过在month_format
中设置PostDayArchiveView
,您可以将预期的月份格式更改为零填充月份号。
class PostDayArchiveView(DayArchiveView):
model = Post
date_field = 'modify_dt'
month_format = '%m'
现在,您可以使用URL模式'archive/<int:year>/<int:month>/<int:day>/'
来接受所需的URL格式。
发布于 2021-01-09 04:55:33
您可以尝试以这种方式获取日期->http://127.0.0.1:8000/blog/archive/2021-01-07
而不是-> http://127.0.0.1:8000/blog/archive/2021/01/07
。这将是一种处理你想要的事情的更简单的方法。
from django.urls import path, register_converter
from datetime import datetime
from . import views
class DateConverter:
regex = '\d{4}-\d{2}-\d{2}'
def to_python(self, value):
return datetime.strptime(value, '%Y-%m-%d')
def to_url(self, value):
return value
register_converter(DateConverter, 'yyyy')
urlpatterns = [
path('', views.index, name='index'),
path('date/<yyyy:date>/', views.date, name='date'),
]
正如this问题中提到的
https://stackoverflow.com/questions/65642799
复制