如何使用路径参数使用单个搜索API(不使用表格) [英] how to have a single search API using path parameters (No form used)

查看:81
本文介绍了如何使用路径参数使用单个搜索API(不使用表格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用此视图搜索word,如下所示:

I have been using this view for searching a word as:

db引用mongo连接(仅用于ref)

db refers mongo connection (just for ref)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/words-<word>', methods=['GET', 'POST'])
def wordsearch(word):
    collection_name=word[0].lower()+'_collection'
    words=db[collection_name]
    data=words.find({'word':word})
    return render_template('wordsearch.html',data=data)

index.html模板中,我一直在这样做以匹配上面的url为:

In index.html template I have been doing this to match this above url as:

    <script type="text/javascript">
        $(document).ready(function(){
         $('#submit').on('click', function() {
              var wordvalue = $("#word").val();  //getting word from element ID 
              window.location.href ="/"+"words-"+wordvalue; //match the URL in view
              })
         });
    </script>

这可以以更动态的方式完成吗?,我的意思是,这仅适用于word,不适用于其他选择或以下选择的组合:

Does this can be done in more dynamic way ?, I mean this only works for word and not for other selections,or combination of selections as below:

搜索输入如下:

word length: ()
word type  : ()
word       : ()
         submit

现在我拥有的API仅在发送word时才匹配,但是我该如何编写一个API以使其匹配所有可能的组合(如word length + word typeword + word type)(我自己定义的查询)

Now the API I have does match only if I send word , but how can I write a single API such that it should match all the possible combinations like word length + word type, word + word type (queries I would define on own)

我尝试过的是:

@app.route('/<n>-letter-words', methods=['GET', 'POST'])
@app.route('/words-<word>', methods=['GET', 'POST'])
def wordsearch(word=None,n=None):
    if word:
        collection_name=word[0].lower()+'_collection'
        words=db[collection_name]
        data=words.find({'word':word})
        return render_template('wordsearch.html',data=data)

    data = 'you are searching with' + n + 'words'
    return render_template('lettersearch.html', data=data)

和模板中的script如:

    <script type="text/javascript">
        $(document).ready(function(){
         $('#submit').on('click', function() {
              var lettervalue = $("#wordlength").val();
              var wordvalue = $("#word").val();
              if (lettervalue==''){
                    window.location.href ="/"+"words-"+wordvalue;
               }
               else{
                window.location.href ="/"+lettervalue+"-letter-words";
               }
              })
         });
    </script>

但是如果有组合之类的话会感到困惑, 6-letter-words-of-verbs动词在这里是word type

But confused if there are combination's like, 6-letter-words-of-verbs verb is a word type here

又如何像我在上述script中使用的JQuery一样,为模板中的这些组合匹配相同的URL?

Also how to match the same URL for these combination's from template as i was doing with JQuery used in script above?

这是正确的方法吗? ,我想在视图中写入所有可能的路由,并将其与模板中的Jquery中的条件进行匹配是一个坏主意,

Is this the correct way? , I guess writing all possible routes in views and match it from template with the conditions in Jquery is a bad idea ,

感谢任何帮助/指导链接,TIA

any help/guiding links are appreciated ,TIA

推荐答案

基于我的建议,建议不要

Based on my comment-suggested recommendation to not ignore the behavior of the client browser, and to follow the intent of the definition of a URI (where /foo+bar and /bar+foo must not represent the same resource), the following is all you actually require, and handles URI-encoding of the values automatically, where your original did not handle URI encoding at all, and requires no additional client-side JavaScript of any kind:

<form action="/search"><input name="q"></form>

从本质上讲,这就是Google(或DuckDuckGo或Yahoo!或…)搜索表单的工作方式.默认方法是GET(使用查询字符串),输入字段的缩写为字段名称" q(query的缩写).使用少量的JS可以绕过表单编码,并直接将查询 as 应用于"查询字符串",但请记住在合并之前对URI/URL编码值/查询/搜索项! (而且这样做可能会绕过您的支持Web框架执行的任何表单数据"收集,例如,您需要自己拔出request.query_string.)

This is essentially how Google's (or DuckDuckGo's, or Yahoo!'s, or…) search form operates. Default method is GET (use a query string), input field given the abbreviated "field name" q (short for query). Using a tiny bit of JS one can bypass the form-encoding and apply the query directly as the "query string" — but remember to URI/URL-encode the value/query/search terms before combining! (And that doing this may bypass any "form data" collection performed by your backing web framework, e.g. you'll need to pull out request.query_string yourself.)

这篇关于如何使用路径参数使用单个搜索API(不使用表格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆