当前位置: 首页>>代码示例>>Python>>正文


Python SearchForm.full_clean方法代码示例

本文整理汇总了Python中forms.SearchForm.full_clean方法的典型用法代码示例。如果您正苦于以下问题:Python SearchForm.full_clean方法的具体用法?Python SearchForm.full_clean怎么用?Python SearchForm.full_clean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在forms.SearchForm的用法示例。


在下文中一共展示了SearchForm.full_clean方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: tree_list

# 需要导入模块: from forms import SearchForm [as 别名]
# 或者: from forms.SearchForm import full_clean [as 别名]
def tree_list(request):
    with get_treestore() as treestore:
        if request.method == 'POST':
            form = SearchForm(request.POST)
            if form.is_valid():
                taxa = form.cleaned_data['taxa']
                filter = form.cleaned_data['filter']
                params = [(x, y) for x, y in form.cleaned_data.items()]
                return redirect('/trees/?' + urllib.urlencode(params))
        else:    
            taxa = request.GET.get('taxa')
            filter = request.GET.get('filter')
            filter = filter if filter else None

            form = SearchForm(initial=request.GET)
            form.full_clean()

        taxa = [x.strip() for x in taxa.split(',')] if taxa else []
        
        params = {'form': form, 'taxonomy':settings.DEFAULT_TAXONOMY}
        params.update(csrf(request))
        
        try:
            trees = list(treestore.list_trees_containing_taxa(contains=taxa, filter=filter, 
                            taxonomy=settings.DEFAULT_TAXONOMY, show_counts=True))
        except Exception as e:
            errors = form._errors.setdefault(NON_FIELD_ERRORS, ErrorList())
            err_msg = "There was a problem with your search: %s" % e
            errors.append(err_msg)
            
            return render_to_response(
                'list.html',
                params,
                context_instance=RequestContext(request)
            )
            
        trees = [(tree_id_from_uri(m[0]), int(m[1])) for m in trees]
        paginator = Paginator(trees, TREES_PER_PAGE)
        
        page = request.GET.get('page')
        try:
            tree_list = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            tree_list = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            tree_list = paginator.page(paginator.num_pages)
        
        params.update({'tree_list': tree_list,
                       'max_match_count': len(taxa)})

        num_pages = paginator.num_pages
        page_range = [n for n in range(tree_list.number - 2, tree_list.number + 3) 
                      if n >= 1 and n <= num_pages]

        params.update({
            'total_results': len(trees),
            'low_result': TREES_PER_PAGE * (tree_list.number - 1) + 1,
            'high_result': min(len(trees), TREES_PER_PAGE * tree_list.number)
            })
        
        if page_range[0] == 2: page_range = [1] + page_range
        elif page_range[0] > 2: page_range = [1, '...'] + page_range
        
        if page_range[-1] == num_pages - 1: page_range += [num_pages]
        elif page_range[-1] < num_pages - 1: page_range += ['...', num_pages]
        
        pages = []
        
        for page in page_range:
            if page == '...': pages.append((page, None))
            else:
                p = []
                for arg in ('taxa', 'filter'):
                    if arg in request.GET:
                        p += [(arg, request.GET.get(arg))]
                p += [('page', page)]
                pages.append((page, '/trees/?' + urllib.urlencode(p)))
        
        params['pages'] = pages
        
        return render_to_response(
            'list.html',
            params,
            context_instance=RequestContext(request)
        )
开发者ID:NESCent,项目名称:phylocommons,代码行数:89,代码来源:views.py


注:本文中的forms.SearchForm.full_clean方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。