2017-04-04 59 views
0

我的問題是,我想顯示例如2016-17,2017-18,2018-19等的所有事件,例如從2016年7月1日至2017年3月31日。 我使用Django 1.6。在特定時間範圍內顯示活動

當我去網址:http://127.0.0.1:8000/en/production/2016-17我得到了一個錯誤: Event matching query does not exist.

事件模型:

class Event(models.Model): 
    name = models.CharField('Name', max_length=255) 
    start = models.DateField('Start') 
    end = models.DateField('End') 

    def __unicode__(self): 
     return self.name 

這裏是我的網址:

url(r'^(?P<branch_slug>/production/(?P<year1>[0-9]{4})-(?P<year2>[0-9]{2})$', EventView.as_view()), 

這是我的觀點:

class EventListView(ListView): 

    def get_queryset(self): 
     filter_ = {'online': True} 
     season_start = Event.objects.get(start=datetime.date(int(self.kwargs['year1']), 7, 1)) 
     season_end = Event.objects.get(end=datetime.date(int(self.kwargs['year2']), 8, 31)) 
     filter_['start__gte'] = season_start 
     filter_['end__lte'] = season_end 
     return self.model.objects.filter(**filter_) 

請尋求幫助或提示。

+0

season_start(和season_end)是Event的一個實例。但是您將它用作日期。試着'過濾_ ['start__gte'] = season_start.start'(和season_end相同)以返回一個Date對象。 – Rafael

+0

@Rafael感謝提示,但我在這個地方得到一個錯誤:'season_start = Event.objects.get(start = datetime.date(int(self.kwargs ['year1']),7,1))' – Mark

+0

對不起。所以,你的數據庫沒有指定日期的事件。 (同樣的錯誤[這裏](http://stackoverflow.com/questions/5508888/matching-query-does-not-exist-error-in-django)) – Rafael

回答

3

您發送的第一年是一個四位數參數,但第二年只有兩個參數。因此,數據庫正在尋找2016年到0017年之間的所有事情,這不太可能會返回任何結果。

爲year2參數使用完整的四位數年份,或者做一些內插初始「20」的操作。