【问题标题】:django previous month view, url for the previous monthdjango上个月查看,上个月的url
【发布时间】:2016-05-17 09:11:51
【问题描述】:

我按照上个月视图 (https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-date-based/) 上的说明进行操作,但它并没有完全显示上个月的网址。

在我的索引页面上,我以特定格式显示当前事件,然后我在顶部有即将到来的和上一个的链接,前一个转到不同的模板,我试图加载前一个月的视图。

如您所见,错误在于网址,我不太确定如何正确获取网址

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'>&nbsp;</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'

【问题讨论】:

  • 我不确定您要在这里做什么。直接的错误是因为您在引号内有过滤器,但即使您将其取出也不起作用;你想格式化什么日期?
  • @DanielRoseman 我只想将上个月的动态发送到视图
  • 您是指从今天开始的最后一个月?
  • @DanielRoseman 是的,谢谢

标签: python django


【解决方案1】:

首先,考虑如何在特定月份(例如 2016 年 4 月)使用 url 标签:

{% url 'maintenance:previous' '2016' '04' %}

如果你想让这个动态,那么你需要一个包含上个月的上下文变量。如果您使用 [MonthArchiveView][1], then Django includesprevious_monthin the template context for you. This is a Python date representing the first day of the previous month. You then use thedate` 过滤器将该日期时间转换为年份和月份字符串。

{% url 'maintenance:previous' previous_month|date:"Y" previous_month|date:"m" %}

请注意,我使用的是“m”,而不是“F”,因为您在网址中使用的是“04”,而不是“April”。

最后,如果您想在索引视图中使用此链接,您需要自己将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,
    })

【讨论】:

  • 我有以前的视图,而不是索引。但是如果这有区别的话,网址在索引模板上吗?每次“Reverse for 'previous' with arguments '(u'', u'')' and keyword arguments '{}' not found”时我都会收到相同的错误
  • 对不起,我不明白你的评论。 u'', 表明 previous_month 未正确包含在模板中。哪个视图在渲染索引模板?
  • 啊,我明白了,页面加载和 url 现在可以工作了 :) 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多