2017-04-16 70 views
0

在我的模板中,我正在使用「搜索」類型的輸入。執行操作時,它將返回頁面「/search_results.html」。來自HTML搜索表單的Django URL

我遇到的問題是它將/?search = yoursearch附加到URL的末尾。

我的URL模式是

url(r'^search_results/(?P<search>\w+)/$, views.SearchView, name='search') 

所以現在,如果我輸入localhost:8000/search_results /蘋果,它會返回包含單詞apple結果。但是,如果我使用搜索欄搜索蘋果,它將返回localhost:8000/search_results /?search = apple,這不是有效的URL。我試過使用

(?P<search>.*) 

取而代之,但它說太多的重定向。

有誰知道如何使用Django的搜索結果中的值?或者有沒有辦法安排我的網址,以便我可以在等號後面解析一下?謝謝

+0

這取決於你的觀點如何連線。 – karthikr

回答

1

不能完全確定你的目標是什麼,但我知道,當匹配的網址Django的忽略查詢字符串(可通過request.META["QUERY_STRING"]請求對象進行訪問。下面是搜索一個小例子處理。
urls.py

from django.conf.urls import url 
from . import views 
urlpatterns = [ 
    url(r'^/search_results',views.search_handler) 

views.py

def search_handler(request): 
    query = {} 
    for i in request.META["QUERY_STRING"].split("&"): 
     query[i.split("=")[0]] = i.split("=")[1] 
    search = query["search"] 
    # your code here 
+0

感謝這似乎正是我想要的,這很簡單!後續問題,在處理多詞搜索時,它會添加+中間詞。如果我搜索「測試這個」,搜索得到值測試+這個。我敢肯定,我可以通過在加號或其他方面分開搜索來弄清楚,但是如果您有任何建議,那就太棒了。 – Chris

+0

@克里斯,我認爲分裂可能是最好的方式。如果這對你有用,你能標記我的答案是否正確?謝謝! –

0

在你的html表單中,你使用方法get或post?

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

在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 

搜索視圖編輯

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) 

現在urls.py所有你需要做的就是在URL模式添加此

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