def get(self, request, *args, **kwargs): """ 搜索 PostgreSQL """ # 关键字参数和分页参数 keyword = request.GET.get("keyword") page = request.GET.get("page", 1) # 查询数据库(数据来自spider) movies = Movie.objects.order_by("rank") if keyword: strict = Q(title__icontains=keyword) | Q(desc__icontains=keyword) | Q(quote__icontains=keyword) movies = movies.filter(strict) pass # 分页 paginator = Paginator(object_list=movies, per_page=10) try: pager = paginator.page(page) except PageNotAnInteger: pager = paginator.page(1) except EmptyPage: pager = paginator.page(paginator.num_pages) pass # 分页片段中使用 pager.queries 达到在翻页时带着查询参数的目的 pager.queries = "keyword=%s" % (request.GET.get("keyword") or "",) # [网页模板]和[通用分页片段(pagination_jinja.html)]中使用 "page" 来访问 Page object context = {} context["page"] = pager return coffin_render(request, "movie.html", context)
def get(self, request, *args, **kwargs): """ 搜索 PostgreSQL """ # 关键字参数和分页参数 keyword = request.GET.get('keyword') page = request.GET.get('page', 1) # 查询数据库(数据来自spider) movies = Movie.objects.order_by('rank') if keyword: strict = Q(title__icontains=keyword) | \ Q(desc__icontains=keyword) | \ Q(quote__icontains=keyword) movies = movies.filter(strict) pass # 分页 paginator = Paginator(object_list=movies, per_page=10) try: pager = paginator.page(page) except PageNotAnInteger: pager = paginator.page(1) except EmptyPage: pager = paginator.page(paginator.num_pages) pass # 分页片段中使用 pager.queries 达到在翻页时带着查询参数的目的 pager.queries = "keyword=%s" % (request.GET.get('keyword') or '', ) # [网页模板]和[通用分页片段(pagination_jinja.html)]中使用 "page" 来访问 Page object context = {} context['page'] = pager return coffin_render(request, 'movie.html', context)
def get(self, request, *args, **kwargs): context = {} context.update(coffin_version="0.4.0") context.update(none_value=None) # print context # {'coffin_version': '0.4.0', 'none_value': None} # 跟 Django 模板不同,Jinja2 模板会将 None 显示为字符串 "None" return coffin_render(request, "jinja2.html", context)
def get(self, request, *args, **kwargs): context = {} context.update(coffin_version='0.4.0') context.update(none_value=None) # print context # {'coffin_version': '0.4.0', 'none_value': None} # 跟 Django 模板不同,Jinja2 模板会将 None 显示为字符串 "None" return coffin_render(request, 'jinja2.html', context)
def get(self, request, *args, **kwargs): """ 搜索 ElasticSearch """ # 关键字参数和分页参数 keyword = request.GET.get("keyword") page = request.GET.get("page", 1) print "----------------------------------------------------------------------------------------" dsl_movie = { "query": { "bool": { "should": [ {"match": {"title": keyword}}, {"match": {"desc": keyword}}, {"match": {"quote": keyword}}, ] } } } # [1] 创建客户端对象 查询ElasticSearch # from pyelasticsearch import ElasticSearch # es = ElasticSearch( # urls='http://192.168.100.100', # port=9200, # ) # [2] ElasticSearch(单例模式) from elasticsearchclient.elasticsearchclient import es_client print ">>>>>>>>>> ElasticSearch(Singleton)" print id(es_client) print ">>>>>>>>>> ElasticSearch(Singleton)" # ElasticSearch 分页参数: es_from, size search = es_client.search(index="douban", doc_type="movie", query=dsl_movie, es_from=0, size=250) # print json.JSONEncoder(indent=4).encode(search) hits = search["hits"]["hits"] movies = [hit["_source"] for hit in hits] print "----------------------------------------------------------------------------------------" # 分页 paginator = Paginator(object_list=movies, per_page=10) try: pager = paginator.page(page) except PageNotAnInteger: pager = paginator.page(1) except EmptyPage: pager = paginator.page(paginator.num_pages) pass # 分页片段中使用 pager.queries 达到在翻页时带着查询参数的目的 pager.queries = "keyword=%s" % (request.GET.get("keyword") or "",) # [网页模板]和[通用分页片段(pagination_jinja.html)]中使用 "page" 来访问 Page object context = {} context["page"] = pager return coffin_render(request, "movie.html", context)
def get(self, request, *args, **kwargs): """ 搜索 ElasticSearch """ # 关键字参数和分页参数 keyword = request.GET.get('keyword') page = request.GET.get('page', 1) print '----------------------------------------------------------------------------------------' dsl_movie = { 'query': { 'bool': { 'should': [{ 'match': { 'title': keyword, } }, { 'match': { 'desc': keyword, } }, { 'match': { 'quote': keyword, } }] } } } # [1] 创建客户端对象 查询ElasticSearch # from pyelasticsearch import ElasticSearch # es = ElasticSearch( # urls='http://192.168.100.100', # port=9200, # ) # [2] ElasticSearch(单例模式) from elasticsearchclient.elasticsearchclient import es_client print '>>>>>>>>>> ElasticSearch(Singleton)' print id(es_client) print '>>>>>>>>>> ElasticSearch(Singleton)' # ElasticSearch 分页参数: es_from, size search = es_client.search( index='douban', doc_type='movie', query=dsl_movie, es_from=0, size=250, ) # print json.JSONEncoder(indent=4).encode(search) hits = search['hits']['hits'] movies = [hit['_source'] for hit in hits] print '----------------------------------------------------------------------------------------' # 分页 paginator = Paginator(object_list=movies, per_page=10) try: pager = paginator.page(page) except PageNotAnInteger: pager = paginator.page(1) except EmptyPage: pager = paginator.page(paginator.num_pages) pass # 分页片段中使用 pager.queries 达到在翻页时带着查询参数的目的 pager.queries = "keyword=%s" % (request.GET.get('keyword') or '', ) # [网页模板]和[通用分页片段(pagination_jinja.html)]中使用 "page" 来访问 Page object context = {} context['page'] = pager return coffin_render(request, 'movie.html', context)