我正在使用Django中的TemplateView实现搜索功能,该类是
GetLastError()
part_list.html
TCHAR lpszFileName[MAX_PATH];
OpenClipboard(0);
APDeviceReport objApd;
HGLOBAL hGlobal = (HGLOBAL)GetClipboardData(CF_HDROP); // Failed when clipboard conatins more than 10000 files
if (hGlobal)
{
HDROP hDrop = (HDROP)GlobalLock(hGlobal);
if (hDrop)
{
UINT fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, 0, 0);
UINT filenameLength;
for (UINT i = 0; i < fileCount; ++i)
{
filenameLength = DragQueryFile(hDrop, i, 0, 0);
DragQueryFile(hDrop, i, lpszFileName, filenameLength+1);
objApd.AddFileList(lpszFileName);
}
GlobalUnlock(hGlobal);
}
}
CloseClipboard();
网址映射
class part_search_view(TemplateView):
model = part_list
context_object_name = 'part_list'
template_name = 'part_list.html'
def get_context_data(self, **kwargs):
context = super(part_search_view, self).get_context_data(**kwargs)
context['my_list'] = populate_nav_bar()
return context
def get_queryset(self):
key = self.request.GET['search_text']
partlist = part_list.objects.filter(Q(part_id__icontains=key) | Q(part_name__icontains=key))
return partlist
serch按钮的表单
{% for part in part_list %}
<a href="{% url 'parts:part_detail' part.id %}" class="list-group-item">{{ part.part_id }} - {{ part.part_name }}</a>
<a href="{% url 'parts:part_update_view' part.id %}" > Edit </a>
{% endfor %}
按下搜索按钮后,地址为
url(r'^search/',views.part_search_view.as_view(),name='part_search_view'),
但是这里没有调用get_query_set(self)调用get_context_data(...),为什么?
答案 0 :(得分:3)
TemplateViews对查询集一无所知,因此他们从不调用get_queryset
方法。您应该创建一个更合适的视图,可能是ListView。
答案 1 :(得分:2)
如果您查看文档,TemplateView没有方法get_queryset()
。然后,您必须在视图中手动调用它。