我按照上个月视图(https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-date-based/)中的说明操作,但它并没有完全显示如何处理上个月的网址。
在我的索引页面上,我有以特定格式显示的当前事件,然后我在顶部显示即将到来的和之前的链接,之前进入另一个模板,其中我试图加载前一个月的视图。 / p>
正如你可以看到错误与网址有关,我不太确定我是如何正确地获取网址
urls.py
from django.conf.urls import url
from . import views
from maintenance.views import EventMonthArchiveView
app_name = 'maintenance'
urlpatterns = [
url(r'^$', views.index, name='index'),
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'),
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,})
class EventMonthArchiveView(MonthArchiveView):
queryset = Maintenance.objects.all()
date_field = "StartTime"
allow_future = False
的index.html
{% extends 'home/base.html' %}
{% block content %}
<h2>IT Maintenance Schedule</h2>
<div id="page-content-header">
<div class="float-left">
<a href="{% url 'maintenance:previous|date:"F Y"' %}"><< Previous Maintenance</a>
</div>
<div class="float-middle">
<a href="{% url 'maintenance:index' %}">Maintenance Today</a>
</div>
<div class="float-right">
<a href="{% url 'maintenance:upcoming' %}">Upcoming Maintenance >></a>
</div>
</div>
<div class='clear'> </div>
<div id='content-body'>
{% for event in Maintenance %}
<p>
{{ event.Title }}
</p>
{% empty %}
<p>There is no maintenance scheulded for today.</p>
{% endfor%}
</div>
{% endblock %}
错误
u'previous|date' is not a registered namespace inside 'maintenance'
Request Method: GET
Request URL: http://it.wrenkitchens.com/maintenance/
Django Version: 1.9.6
Exception Type: NoReverseMatch
Exception Value:
u'previous|date' is not a registered namespace inside 'maintenance'
答案 0 :(得分:2)
首先,考虑如何使用特定月份的网址标记,例如2016年4月:
{% url 'maintenance:previous' '2016' '04' %}
如果要使其动态化,则需要包含上个月的上下文变量。如果您使用[MonthArchiveView][1], then Django includes
previous_month in the template context for you. This is a Python date representing the first day of the previous month. You then use the
日期`过滤器将该日期时间转换为年和月字符串。
{% url 'maintenance:previous' previous_month|date:"Y" previous_month|date:"m" %}
请注意,我使用了&#39; m&#39;而不是&#39; F&#39;,因为您使用的是&#39;&#39;而不是&#39; 4月和#39 ;在网址中。
最后,如果您想在索引视图中使用此链接,则需要自己将previous_month
传递给模板上下文。您可以通过获取当月的第一天,然后减去一天来获取上个月的最后一天。因为你只需要
from datetime import date, timedelta
def index(request):
today = date.today()
previous_month = (today.replace(day=1) - timedelta(1)).replace(day=1)
ObjMaintenance = Maintenance.objects.filter(StartTime__gt=today)
return render(request, 'maintenance/index.html', {
'Maintenance': ObjMaintenance,
'previous_month': previous_month,
})