Skip to main content
 首页 » 编程设计

python之django之eventmonthview 404 而不是空模板并获取前一个月

2025年05月04日34dyllove98

我正在使用 MonthArchiveView 并使用 url 发送上个月的年份和日期。

但是,当我点击它时,我得到一个 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 

我不知道为什么它不显示空白页而不是这个?我不应该得到 is empty 模板而不是这个吗?

我还想知道如何从 url 中获取当前月份/年份,如果用户再次单击上一个链接,则减去 1 个月,然后再一次(每次用户单击上一个,他们都会得到上个月

View .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    

网址.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>   

请您参考如下方法:

要在没有事件时显示空列表而不是 404,请设置 allow_empty为您的月存档 View 设置为 True。

class EventMonthArchiveView(MonthArchiveView): 
    template_name = "maintenance/previous.html" 
    queryset = Maintenance.objects.all() 
    date_field = "StartTime" 
    allow_future = False    
    allow_empty = True