當前位置: 首頁>>代碼示例>>Python>>正文


Python SearchForm.save方法代碼示例

本文整理匯總了Python中search.forms.SearchForm.save方法的典型用法代碼示例。如果您正苦於以下問題:Python SearchForm.save方法的具體用法?Python SearchForm.save怎麽用?Python SearchForm.save使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在search.forms.SearchForm的用法示例。


在下文中一共展示了SearchForm.save方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: home

# 需要導入模塊: from search.forms import SearchForm [as 別名]
# 或者: from search.forms.SearchForm import save [as 別名]
def home(request):
    title = "Search text"
    form = SearchForm(request.POST or None)

    context = {
        "title": title,
        "form": form
    }

    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()

        message = "You will get search results for: %s via %s soon" % (instance.searching_text, instance.email)
        context = {
            "title": "Thank you",
            "message": message,
        }

        with ix.searcher() as searcher:
            query = QueryParser("text", ix.schema).parse(instance.searching_text)
            # Get a collector object
            c = searcher.collector(limit=None)
            # Wrap it in a TimeLimitedCollector and set the time limit to 10 seconds
            tlc = TimeLimitCollector(c, timelimit=instance.t_limit)
            # Try searching
            try:
                searcher.search_with_collector(query, tlc)
            except TimeLimit:
                pass
            # You can still get partial results from the collector
            results = tlc.results()
            lst = []
            for i in range(0, len(results)):
                st = ''
                st += 'Book: '
                st += results[i]["book"]
                st += ', chapter: '
                st += results[i]["chapter"]
                st += ', page: '
                st += str(results[i]["page"])
                lst.append(st)



        # with ix.searcher() as searcher:
        #     query = QueryParser("text", ix.schema).parse(instance.searching_text)
        #     results = searcher.search(query)
        #     lst = []
        #     for i in range(0, len(results)):
        #         st = ''
        #         st += 'Book: '
        #         st += results[i]["book"]
        #         st += ', chapter: '
        #         st += results[i]["chapter"]
        #         st += ', page: '
        #         st += str(results[i]["page"])
        #         lst.append(st)

        logging.basicConfig(format=u'%(levelname)-8s [%(asctime)s] %(message)s', level=logging.DEBUG,
                            filename=u'mylog.log')
        time_diff = datetime.datetime.now(timezone.utc) - instance.timestamp
        logging.info(time_diff.total_seconds())

        subject = 'Search results for: ' + form.cleaned_data.get('searching_text')
        message = 'Search results for: ' + form.cleaned_data.get('searching_text') + '\n'
        for i in range(0, len(lst)):
            message += str(i+1)+') '
            message += lst[i]
            message += '\n'
        from_email = settings.EMAIL_HOST_USER
        to_email = form.cleaned_data.get('email')
        send_mail(subject,
                  message,
                  from_email,
                  [to_email],
                  fail_silently=True)
    return render(request, "home.html", context)
開發者ID:PielliaVasyl,項目名稱:search_text,代碼行數:80,代碼來源:views.py


注:本文中的search.forms.SearchForm.save方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。