我需要创建基于月份的视图。我可以按月过滤我的输入项目,我可以在html页面上显示它们。我正在努力做的只是显示您从主页面选择的月份数据。例如,如果您点击11月,它会转到仅显示11月项目的页面,依此类推。我需要一个特定的url配置吗?我希望有一种更简单的方法来做到这一点,而不是每个月创建一个视图,然后创建每个月的网址...
urls.py
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'entry/list/?P<month_here>$', views.EntryListView.as_view(), name='entry-list'),
]
views.py
class EntryListView(generic.ListView):
template_name = 'argent/index_list.html'
context_object_name = 'object_list'
def get_queryset(self):
return Entry.objects.all()
def get_context_data(self, **kwargs):
mth = super(EntryListView, self).get_context_data(**kwargs)
# ENTRY BY MONTH
# January17
mth['January17_qs'] = Entry.objects.filter(date__range=('2017-1-1', '2017-1-31'))
# February17
mth['February17_qs'] = Entry.objects.filter(date__range=('2017-2-1', '2017-2-28'))
return ctx
答案 0 :(得分:0)
您可以从url解析args。以上内容将在您的视图中添加一个名称&#34; month&#34;的查询参数,该单词应该是一个单词(\ w + regex)
month = self.kwargs.get('month')
然后在get_context_data中你应该在你的kwargs中有这个:
def get_context_data(self, **kwargs):
mth = super(EntryListView, self).get_context_data(**kwargs)
# ENTRY BY MONTH
month = self.kwargs.get('month')
# January17
if month == 'january:
mth['January17_qs'] = Entry.objects.filter(date__range=('2017-1-1', '2017-1-31'))
elif month == 'february':
# February17
mth['February17_qs'] = Entry.objects.filter(date__range=('2017-2-1', '2017-2-28'))
return mth
然后添加逻辑,解析你的args然后返回正确的查询集。
for year in range(2016, 2019):
mth["{}{}_qs".format(month.title(), str(year)[-2:])] = Entry.objects.filter(date__range=("{}-1-1".format(year), "{}-1-31".format(year))
# this will add you a 'January16_qs', 'January17_qs', 'January18_qs' variables if month is 'january'
多年来你还可以添加一些for-clausule来构建你的参数:
GRIB_API ERROR : jpeg support not enabled. Please rerun configure with the --with-jasper or --with-openjpeg option.
GRIB_API ERROR : unable to get codedValues as double array (Function not yet implemented)
GRIB_API ERROR : unable to get values as double array (Function not yet implemented)
Traceback (most recent call last):
File "iterator.py", line 43, in main
example()
File "iterator.py", line 16, in example
iterid = grib_iterator_new(gid,0)
File "/program/gribapi.py", line 70, in modified
return _func_(**kw)
File "/program/gribapi.py", line 495, in grib_iterator_new
GRIB_CHECK(err)
File "/program/gribapi.py", line 70, in modified
return _func_(**kw)
File "/program/gribapi.py", line 143, in GRIB_CHECK
raise GribInternalError(errid)
GribInternalError: Function not yet implemented