2006年11月06日 星期一 17:47
ÏÖÔÚÎÒʹÓõÄÊÇlimodouÐÖÔÚwoodlogÖеÄËÑË÷´úÂ룬ÈçÏ£º def search(request): d = {} if request.REQUEST: username = request.REQUEST.get('username', '') keyword = request.REQUEST.get('keyword', '') d['keyword'] = keyword object_list = None prefix_url = '/search/?' if keyword: words = keyword.split() condition = [] keys = [] for key in words: key = key.strip() if key: condition.append(Q(title__icontains=key) | Q(body__icontains=key)) keys.append(key) if condition: object_list = Entry.objects.entries().filter(*condition) keyword = ' '.join(keys) prefix_url += 'keyword=%s' % keyword if username: user = User.objects.get(username=username) object_list = object_list.filter(user=user.id) prefix_url += '&username;=%s' % username d['username'] = username if object_list: page = Page(request, object_list, paginate_by=20, urlprefix= urllib.quote_plus(prefix_url)) d['page'] = page d['keyword'] = keyword d['blogcount'] = len(object_list) context = RequestContext(request, d) return theme_render_to_response('search/search.html', context_instance=context) ÎÒÓвâÊÔÌõÄ¿A£¬B£¬C£¬·Ö±ðÈçÏ£º A name "test" profile "ÖйúÈËÃñÒøÐÐ" B name "temp" profile "ÔÚÃÀ¹úµÄÖйúÈË" C name "Ó¢¹úÈË" profile "Ó¢¹úÈËдÏÂtest" È»ºóËÑË÷£ºtest »á·µ»ØÌõÄ¿A ËÑË÷£ºtemp Öйú »á·µ»ØÌõÄ¿B µ«ÊÇÎÒËÑË÷ £º test ÃÀ¹ú Ó¢¹ú Ôò²»»á·µ»ØÈý¸öÌõÄ¿£¬Ôõô»ØÊÂÄØ£¿ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061106/0b0ad21c/attachment.html
2006年11月07日 星期二 17:26
ÎÊÌâ½â¾öÁË£¬ÎÒ½«condition.append£¨¡¡£©Ê¡ÂÔ²¿·Ö×÷Ϊ×Ö·û´® È»ºóÓÃ" | ".join·Ö¸îÁбí ÔÙÓÃcompile±àÒë ×îºó²ÉÓÃevalÖ´ÐÐ×Ö·û´® ºÇºÇ£¬Ð´Ò»Ï£¬´æ¸öµµ¡£ Ì«¼òª£¬Èç¹ûÓÐÅóÓÑÓкð취ÀÍ·³Ö¸µã¡£ ÔÚ06-11-6£¬·çÏò±ê <vaneoooo在gmail.com> дµÀ£º > > > ÏÖÔÚÎÒʹÓõÄÊÇlimodouÐÖÔÚwoodlogÖеÄËÑË÷´úÂ룬ÈçÏ£º > > def search(request): > d = {} > if request.REQUEST: > username = request.REQUEST.get('username', '') > keyword = request.REQUEST.get('keyword', '') > d['keyword'] = keyword > object_list = None > > prefix_url = '/search/?' > if keyword: > words = keyword.split() > condition = [] > keys = [] > for key in words: > key = key.strip() > if key: > condition.append(Q(title__icontains=key) | > Q(body__icontains=key)) > keys.append(key) > if condition: > object_list = Entry.objects.entries().filter(*condition) > keyword = ' '.join(keys) > prefix_url += 'keyword=%s' % keyword > if username: > user = User.objects.get(username=username) > object_list = object_list.filter(user=user.id) > prefix_url += '&username;=%s' % username > d['username'] = username > if object_list: > page = Page(request, object_list, paginate_by=20, urlprefix= > urllib.quote_plus(prefix_url)) > d['page'] = page > d['keyword'] = keyword > d['blogcount'] = len(object_list) > context = RequestContext(request, d) > return theme_render_to_response('search/search.html', > context_instance=context) > > > ÎÒÓвâÊÔÌõÄ¿A£¬B£¬C£¬·Ö±ðÈçÏ£º > > A > name "test" > profile "ÖйúÈËÃñÒøÐÐ" > > B > name "temp" > profile "ÔÚÃÀ¹úµÄÖйúÈË" > > C > name "Ó¢¹úÈË" > profile "Ó¢¹úÈËдÏÂtest" > > È»ºóËÑË÷£ºtest > »á·µ»ØÌõÄ¿A > > ËÑË÷£ºtemp Öйú > »á·µ»ØÌõÄ¿B > > µ«ÊÇÎÒËÑË÷ £º test ÃÀ¹ú Ó¢¹ú > > Ôò²»»á·µ»ØÈý¸öÌõÄ¿£¬Ôõô»ØÊÂÄØ£¿ > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061107/224abef2/attachment.htm
2006年11月07日 星期二 23:36
呵呵,代码贴出来看看啦,不知道这样执行的效率会不会打折扣。 在 06-11-7,风向标<vaneoooo at gmail.com> 写道: > > 问题解决了,我将condition.append(……)省略部分作为字符串 > 然后用" | ".join分割列表 > 再用compile编译 > 最后采用eval执行字符串 > > 呵呵,写一下,存个档。 > 太简陋,如果有朋友有好办法劳烦指点。 > > > 在06-11-6,风向标 <vaneoooo at gmail.com> 写道: > > > > > > 现在我使用的是limodou兄在woodlog中的搜索代码,如下: > > > > def search(request): > > d = {} > > if request.REQUEST: > > username = request.REQUEST.get('username', '') > > keyword = request.REQUEST.get('keyword', '') > > d['keyword'] = keyword > > object_list = None > > > > prefix_url = '/search/?' > > if keyword: > > words = keyword.split() > > condition = [] > > keys = [] > > for key in words: > > key = key.strip() > > if key: > > condition.append(Q(title__icontains=key) | > Q(body__icontains=key)) > > keys.append(key) > > if condition: > > object_list = Entry.objects.entries().filter(*condition) > > keyword = ' '.join(keys) > > prefix_url += 'keyword=%s' % keyword > > if username: > > user = User.objects.get(username=username) > > object_list = object_list.filter(user=user.id) > > prefix_url += '&username;=%s' % username > > d['username'] = username > > if object_list: > > page = Page(request, object_list, paginate_by=20, > urlprefix=urllib.quote_plus(prefix_url)) > > d['page'] = page > > d['keyword'] = keyword > > d['blogcount'] = len(object_list) > > context = RequestContext(request, d) > > return theme_render_to_response('search/search.html', > context_instance=context) > > > > > > 我有测试条目A,B,C,分别如下: > > > > A > > name "test" > > profile "中国人民银行" > > > > B > > name "temp" > > profile "在美国的中国人" > > > > C > > name "英国人" > > profile "英国人写下test" > > > > 然后搜索:test > > 会返回条目A > > > > 搜索:temp 中国 > > 会返回条目B > > > > 但是我搜索 : test 美国 英国 > > > > 则不会返回三个条目,怎么回事呢? > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese >
Zeuux © 2025
京ICP备05028076号