django视图和模板未集成到urls.py

时间:2018-08-14 14:03:57

标签: django django-models django-templates django-views

我尝试访问的URL是blog / 2018/08和blog / 2018/08/14。 其他网址,例如blog /,blog / 2018,blog / today,archive /效果很好 但是包含月份和日期的网址不起作用。

这是我的代码:

blog / 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中提到了我得到的所有模板!

非常感谢

1 个答案:

答案 0 :(得分:0)

这里的问题是月份格式。日期视图默认使用日期的缩写字符串格式-英文,“ jan”,“ feb”等。但是您使用的是整数,因此需要告知视图:

class PostMAV(MonthArchiveView):
    model = Post
    date_field = 'modify_date'
    template_name = 'blog/post_archive_month.html'
    month_format = '%m'