来自YearArchiveView中列出的月份的超链接

时间:2014-07-30 14:49:59

标签: django django-views

我有一个YearArchiveView页面,其中显示了一年中的月份列表。我正在尝试设置每个月的超链接到该月的页面,该页面列出了当月的所有活动。

我无法摆脱以下错误消息:

  

NoReverseMatch at / booking / 2014 /

     

反转' booking-month-archive'参数'(u' 2014/01',)'和关键字参数' {}'未找到。尝试了1种模式:['预订/(?P \ d {4})/(?P \ d {2})/ $']

我的views.py:

class BookingsThisYear(YearArchiveView):

    queryset = Booking.objects.all()
    date_field = 'start_date'
    make_object_list = True
    allow_empty = False
    allow_future = False


class BookingsThisMonth(MonthArchiveView):

    queryset = Booking.objects.all().order_by('start_date')
    date_field = 'start_date'
    make_object_list = True
    allow_empty = False
    allow_future = False

urls.py:

    url(r'^booking/(?P<year>\d{4})/$', 
        BookingsThisYear.as_view (
        template_name = 'guests/booking_archive_year.html'),
        name = 'booking-year-archive'),

    url(r'^booking/(?P<year>\d{4})/(?P<month>\d{2})/$', 
        BookingsThisMonth.as_view (month_format='%m'),
        name = 'booking-month-archive'),        
        )

和我的模板代码:

booking_year_archive.html:

<!DOCTYPE html>
<html lang="en">

{% extends 'base.html' %}
{% load staticfiles %}

{% block col1 %}
<div class="col-md-12">

    {% for date in date_list %}
        {% if date_list %}
            <a href="{% url 'booking-month-archive' date|date:'Y/m' %}"> </a>{{ date|date:"M" }} | </a>
        {% endif %}
    {% endfor %}

</div><!--col-md-12-->
{% endblock %}

booking_month_archive.html

    <div class="col-md-9">
    <h3>{{ month|date:"F Y" }}</h3>
    <table class="table table-striped table-bordered">
        <thead>
            <th style="width 30%">Dates</th>
            <th style="width 30%">Name</th>
            <th style="width 40%">Comments</th>
        </thead>
            {% for obj in object_list %}
            <tr>
                <td>{{ obj.start_date|date:"F j, Y" }}</td>
                <td>{{ obj.guest }}</td>
                <td>{{ obj.notes }}</td>
            </tr>
            {% endfor %}
    </table>

任何帮助非常感谢。我应该说当我进入预订/ 2014/07&#39;在网址栏中,它会完美地加载month_archive_view.html页面。

2 个答案:

答案 0 :(得分:0)

由于您的视图需要两个参数,一年和一个月而不是一个日期字符串,您可以将月份和日期作为关键字参数发送到{% url %}标记

{% url 'booking-month-archive' year='2014' month='7' %}

或正常的位置参数

{% url 'booking-month-archive' '2014' '7' %}

答案 1 :(得分:0)

'2014/01'是一个论点。您的观点需要两个 - &gt; '2014''01'

尝试

{% url 'booking-month-archive' year=date|date:'Y' month=date|date:'m' %}

{% url %}会将其转换为booking/2014/01