2013-06-19 170 views
4

從我的模板: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但是被調用的視圖需要這些參數,因此鏈接失敗。

回答

4

您如何計劃將這些參數嵌入到您的網址中?沒有什麼可以捕獲它們,也沒有辦法用反向查找來構建它。你需要一個接受這些參數的URL模式。喜歡的東西:這裏

url(r'^ocal/(?P<year>\d{4})/(?P<month>(0|1)?\d)/$', views.calendar, name = "othermonth_bymonth"), 

使用關鍵字,而不是位置參數是可選的,但我認爲它使事情變得更容易 - 並且允許可引起類似的展示自行指定沒事的時候當天的日曆動作設定的默認值。

此外,您mytickets查詢集也可以這樣構成:

mytickets = Event.objects.filter(on_sale__year=year, onsale__month=month) 

我認爲這是一個有點清潔閱讀。

其實,仔細看看 - Django內置的基於日期的視圖可以爲你做很多事情。如果您尚未看着他們,我建議這樣做:

https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-date-based/

對於這個特殊的視圖,所有你需要做的是子類MonthArchiveView創建您TicketCalendar實例,並把它添加到您的上下文。

編輯:好的,你仍然有問題。這是我會怎麼去解決這樣的:

views.py 
class TicketMonthArchiveView(MonthArchiveView): 
    allow_empty = True # show months even in which no tickets exist 
    allow_future = True # show future months 
    model = Event 

    def get_context_data(self, **kwargs): 
     base_context = super(TicketMonthArchiveView, self).get_context_data(**kwargs) 
     my_tickets = kwargs['object_list'] 
     base_context['calendar'] = mark_safe(TicketCalendar(my_tickets).formatmonth(self.get_year(), self.get_month())) 
     # the above could be a little off because I don't know exactly how your formatmonth method works 
     return base_context 

urls.py 
    from somewhere.views import TicketMonthArchiveView 
    # other patterns, plus: 
    url(r'^ocal/(?P<year>\d{4})/(?P<month>(0|1)?\d)/$', TicketMonthArchiveView.as_view(), name='calendar_by_month'), 

template event_archive_month.html 
    <a href="{% url 'tracker:calendar_by_month' next_month|date:'%Y' next_month|date:'%m' %}">{{ next_month|date:'%f' }}</a> 

顯然有很多更你可以用年份和日期的意見在這裏做,但是這應該表現的一般概念。

+0

我採納了你的建議並嘗試了django的基於日期的視圖,但是當我嘗試創建下個月的鏈接時遇到同樣的問題。嘗試傳遞模板中的參數會創建一個NoReverseMatch,我不知道如何去做這件事。 – apardes

+0

我已經添加了一個展開的解決方案,展示瞭如何將MonthArchiveView用於我的答案。 –

+0

感謝您的澄清。對不起,我不能投票,但+1 – apardes

0

儘管使用了正確的語法,但大型上下文卷也對我造成了這種行爲,

我有這個,對Django的1.5:

urls.py

url(r'(?P<CLASSID>[A-Z0-9_]+)/$', 'psclassdefn.views.psclassdefn_detail', name="psclassdefn_detail"), 

模板。HTML

{% for inst in li_inst %} 
<li><a href={% url 'psclassdefn.views.psclassdefn_detail' CLASSID=inst.CLASSID %}></a></li> 
{% endfor %} 

我一直在得到NoReverseMatch即使語法看起來OK,我可以扭轉成一個無參數的URL。

現在,li_inst是從db中獲取的約1000行的巨大列表,並傳遞給上下文變量。爲了測試,我通過刪除了大約10行左右的行來修剪列表。它工作,沒有任何語法變化。

也許模板系統有意遏制上下文大小?如果需要,我可以過濾列表,只是沒有想到這個錯誤來自它。