Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 来自HTML搜索表单的Django URL_Python_Html_Django - Fatal编程技术网

Python 来自HTML搜索表单的Django URL

Python 来自HTML搜索表单的Django URL,python,html,django,Python,Html,Django,在我的模板中,我将输入与类型搜索一起使用。执行操作时,将返回页面/search_results.html 我遇到的问题是,它将/?search=yoursearch附加到URL的末尾 我的URL模式是 url(r'^search_results/(?P<search>\w+)/$, views.SearchView, name='search') 所以现在如果我输入localhost:8000/search\u results/apple,它将返回包含单词apple的结果。但是如果

在我的模板中,我将输入与类型搜索一起使用。执行操作时,将返回页面/search_results.html

我遇到的问题是,它将/?search=yoursearch附加到URL的末尾

我的URL模式是

url(r'^search_results/(?P<search>\w+)/$, views.SearchView, name='search')
所以现在如果我输入localhost:8000/search\u results/apple,它将返回包含单词apple的结果。但是如果我使用搜索栏搜索苹果,它会返回localhost:8000/search\u results/?search=apple,这不是一个有效的URL。我试着用

(?P<search>.*)
相反,它说重定向太多了


有人知道如何在Django中使用搜索结果中的值吗?或者有没有办法安排我的URL,这样我就可以解析等号后面的位?谢谢

在您的html表单中,您使用的是get方法还是post方法

<form method="post">
</form>

不完全确定您的目标是什么,但我知道当匹配URL时,django会忽略可以通过request.META[query\u string]在请求对象中访问的查询字符串。下面是一个用于搜索的小示例处理程序。 url.py

views.py

def搜索\处理请求: 查询={} 对于请求中的i.META[QUERY\u STRING].split&: 查询[i.split=[0]]=i.split=[1] 搜索=查询[搜索] 你的代码在这里 In views.py

# no need to edit this
def normalize_query(query_string,
                findterms=re.compile(r'"([^"]+)"|(\S+)').findall,
                normspace=re.compile(r'\s{2,}').sub):
    ''' Splits the query string in invidual keywords, getting rid of 
        unecessary spaces and grouping quoted words together.
    '''
    return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)]


# no need to edit this
def get_query(query_string, search_fields):
    ''' Returns a query, that is a combination of Q objects. That combination aims to search keywords within a model by testing the given search fields.'''

    query = None # Query to search for every search term
    terms = normalize_query(query_string)

    for term in terms:
        or_query = None # Query to search for a given term in each field
        for field_name in search_fields:
            q = Q(**{"%s__icontains" % field_name: term})
            if or_query is None:
                or_query = q
            else:
                or_query = or_query | q
        if query is None:
            query = or_query
        else:
            query = query & or_query
    return query
要编辑的搜索视图 现在在urls.py中,您需要做的就是将其添加到url模式中

url(
    regex=r'^search/$',
    view = search,
    name = 'search'
  ),

这取决于你的观点是如何连接的。谢谢,这似乎正是我想要的,而且非常简单!后续问题是,在处理多词搜索时,会在词之间添加一个+符号。如果我搜索测试这个,搜索得到值test+this。我相信我可以通过用加号或其他符号分割搜索来解决这个问题,但如果你有任何建议,那就太好了。@Chris,我认为分割可能是最好的方法。如果这对你有用的话,你能把我的答案标对吗?谢谢
def search(request):
    books = Mymodel.objects.all()
    query_string = ''
    found_entries = None
    source = ""

    # the 'search' in this request.GET is what appears in the url like
    #localhost:8000/?search=apple. 
    if ('search' in request.GET) and request.GET['search'].strip():
        query_string = request.GET['q']
        entry_query = get_query(query_string, [list, of, model, field, to, search])
    found_entries = Mymodel.objects.filter(entry_query)

    context = {
        'query_string': query_string,
        'found_entries': found_entries,
    }

    return render(request, 'pathto/search.html', context)
url(
    regex=r'^search/$',
    view = search,
    name = 'search'
  ),