即时通讯使用MonthArchiveView并使用网址发送上个月的年份和日期。
然而,当我点击它时,我得到一个带有以下内容的404:
Page not found (404)
Request Method: GET
Request URL: http://it.service.com/maintenance/previous/2016/04/
Raised by: maintenance.views.EventMonthArchiveView
No Planned IT Maintenance available
我不知道为什么它没有显示空页而不是这个?我不应该得到空模板而不是这个吗?
我还想知道如果用户再次点击上一个链接,并且再次(每次用户点击上一次他们获得上个月)时,如何从网址中扣除当前月/年,减去1个月p>
views.py
from django.views.generic.list import ListView
from django.shortcuts import get_object_or_404, render, render_to_response
from django.http import HttpResponse
from datetime import date, datetime, timedelta, time
from django.views.generic.dates import MonthArchiveView
from .models import Maintenance
from .models import MaintenanceType
from .models import ServiceType
# Create your views here.
def index(request):
today = date.today()
ObjMaintenance = Maintenance.objects.filter(StartTime__gt=today)
return render(request, 'maintenance/index.html', {
'Maintenance': ObjMaintenance,
})
def thirtydays(request):
today = date.today()
previous_month = (today.replace(day=1) - timedelta(1)).replace(day=1)
ObjMaintenance = Maintenance.objects.filter(StartTime__gt=today-timedelta(days=30))
return render(request, 'maintenance/previous.html', {
'Maintenance': ObjMaintenance,
'previous_month': previous_month,
'Title': 'Maintence in the Last 30 Days',
})
def previous(request, year, month):
previousMnths = datetime
ObjMaintenance = Maintenance.objects.filter(StartTime__gt=today-timedelta(days=30))
strPrevious = 'Maintence for the month %s' % (previous_month)
return render(request, 'maintenance/previous.html', {
'Maintenance': ObjMaintenance,
'previous_month': previous_month,
'Title': strPrevious,
})
def upcoming(request):
today = date.today()
ObjMaintenance = Maintenance.objects.filter(StartTime__gt=today+timedelta(days=30))
return render(request, 'maintenance/index.html', {'Maintenance': ObjMaintenance,})
class EventMonthArchiveView(MonthArchiveView):
template_name = "maintenance/previous.html"
queryset = Maintenance.objects.all()
date_field = "StartTime"
allow_future = False
urls.py
app_name = 'maintenance'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^thirtydays/$', views.thirtydays, name="thirtydays"),
url(r'^previous/(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$',
EventMonthArchiveView.as_view(month_format='%m'),
name="previous"),
url(r'^upcoming$', views.upcoming, name='upcoming'),
]
模板网址
<a href="{% url 'maintenance:previous' previous_month|date:'Y' previous_month|date:'m' %}"><< Previous Maintenance</a>
答案 0 :(得分:5)
要在没有活动时显示空列表而不是404,请为您的月份归档视图设置allow_empty
至True
。
class EventMonthArchiveView(MonthArchiveView):
template_name = "maintenance/previous.html"
queryset = Maintenance.objects.all()
date_field = "StartTime"
allow_future = False
allow_empty = True