從我的模板:Django的無反向匹配與參數
<a href="{% url 'tracker:othermonth' year next_month %}">July</a>
URL模式:
url(r'^ocal/$', views.calendar, name = "othermonth"),
查看:
def calendar(request, year, month):
my_year = int(year)
my_month = int(month)
my_calendar_from_month = datetime(my_year, my_month, 1)
my_calendar_to_month = datetime(my_year, my_month, monthrange(my_year, my_month)[1])
my_tickets = Event.objects.filter(on_sale__gte=my_calendar_from_month).filter(on_sale__lte=my_calendar_to_month)
my_previous_year = my_year
my_previous_month = my_month - 1
if my_previous_month == 0:
my_previous_year = my_year - 1
my_previous_month = 12
my_next_year = my_year
my_next_month = my_month + 1
if my_next_month == 13:
my_next_year = my_year + 1
my_next_month = 1
my_year_after_this = my_year + 1
my_year_before_this = my_year - 1
cal = TicketCalendar(my_tickets).formatmonth(year, month)
return render_to_response('calendar.html', {'events_list': my_tickets,
'calendar': mark_safe(cal),
'month': my_month,
'month_name': named_month(my_month),
'year': my_year,
'previous_month': my_previous_month,
'previous_month_name': named_month(my_previous_month),
'previous_year': my_previous_year,
'next_month': my_next_month,
'next_month_name': named_month(my_next_month),
'next_year': my_next_year,
'year_before_this': my_year_before_this,
'year_after_this': my_year_after_this,
}, context_instance=RequestContext(request))
錯誤:
Reverse for 'othermonth' with arguments '(2013, 7)' and keyword arguments '{}' not found.
我已經通過stackoverflow和django文檔搜索,但我似乎無法弄清楚爲什麼我得到這個NoReverseMatch
錯誤。我敢肯定它對我來說是一個非常簡單的監督,因爲我正盯着之前幾乎與此相同的項目的代碼,並且工作正常。任何幫助將不勝感激,謝謝。
更新:我試圖刪除我試圖發送的URL參數,並修復了NoReverseMatch
但是被調用的視圖需要這些參數,因此鏈接失敗。
我採納了你的建議並嘗試了django的基於日期的視圖,但是當我嘗試創建下個月的鏈接時遇到同樣的問題。嘗試傳遞模板中的參數會創建一個NoReverseMatch,我不知道如何去做這件事。 – apardes
我已經添加了一個展開的解決方案,展示瞭如何將MonthArchiveView用於我的答案。 –
感謝您的澄清。對不起,我不能投票,但+1 – apardes