我有一个django项目,其中一个视图使用与urls.py中指定的参数不同的参数调用render_to_response。当我尝试在其他地方做类似的事情时,我得到一个错误,说该视图需要特定数量的参数。怎么会这样呢?
url(r'^report_view/(?P<report_type>.*)/(?P<date_from>.*)/(?P<date_to>.*)/$', report_view),
url(r'^scanner_times/(?P<scanner_id>\d+)/$', scanner_times),
来自我的模板
来自views.py
def scanner_times(request, scanner_id):
current_scanner = get_object_or_404(Scanner, pk=scanner_id)
tickets = Ticket.objects.filter(scanner=scanner_id).order_by('date_requested')
# loop over each ticket, adding a new entry to the list if it's the first ticket for that day, or incrementing if the day is already in it.
userid = request.user.username
date_count = {}
for ticket in tickets:
if ticket.approved:
datestr = str(ticket.date_scheduled)
datestr = datestr[:10]
if datestr in date_count:
date_count[datestr] += 1
else:
date_count[datestr] = 1
sorted_date_count = sorted(date_count.items(), key = itemgetter(0))
return render_to_response('scanner_times.html',
{'tickets': tickets,
'current_scanner':current_scanner,
'date_count':date_count,
'sorted_date_count':sorted_date_count},
context_instance=RequestContext(request))
怎么办?我很困惑!
答案 0 :(得分:1)
我认为那里存在一些混乱。匹配URL后,您将使用render_to_response
,并且您位于为该网址提供服务的视图中。
这是render_to_response
:
render_to_response(template_name, context=None, context_instance=_context_instance_undefined,
content_type=None, status=None, dirs=_dirs_undefined, using=None)
在你的情况下:
return render_to_response('scanner_times.html',
{'tickets': tickets,
'current_scanner':current_scanner,
'date_count':date_count,
'sorted_date_count':sorted_date_count},
context_instance=RequestContext(request))
此处,您的模板为'scanner_times.html'
,然后您拥有上下文字典,然后您拥有context_instance
。
此函数会将此上下文传递给模板,呈现'scanner_times.html'
并将其返回。
此时与urls
无关。