2014-09-12 116 views
0

我有這個html搜索表單,我想將它鏈接到我的搜索視圖以使其工作。在django中執行搜索

<form class="navbar-form navbar-left" role="search" > 
    <div class="form-group"> 
     <input type="text" class="form-control" placeholder="Search" action = "/search/"> 
    </div> 
    <button type="submit" class="btn btn-default">Submit</button> 
</form> 

這裏是意見。

def searchResults(request, drname): 
    # drname = request.GET.get('drname') 
    doctors = Doctor.objects.filter(name__contains=drname) 
    clinic = Doctor.objects.filter(clinic__name__contains=drname) 
    d = getVariables(request) 
    d['doctors'] = doctors 
    d['doctors_by_clinic'] = doctors 
    return render_to_response('meddy1/doclistings.html',d) 

urls.py

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

回答

3
  1. Html。 <form>必須具有action屬性,而不是<input>。此外,將搜索字符串作爲GET參數,不包含在URL(添加name屬性<input>):

    <form class="navbar-form navbar-left" role="search" action="/search/"> 
        <div class="form-group"> 
         <input type="text" class="form-control" placeholder="Search" name="drname"> 
        </div> 
        <button type="submit" class="btn btn-default">Submit</button> 
    </form> 
    
  2. urls.py.刪除該零件(?P<drname>\w+)/

    url(r'^search/$', views.searchResults, name='searchResults'), 
    
  3. views.py。從GET參數獲取搜索字符串:

    def searchResults(request): 
        drname = request.GET.get('drname') 
        doctors = Doctor.objects.filter(name__contains=drname) 
        clinic = Doctor.objects.filter(clinic__name__contains=drname) 
        d = getVariables(request) 
        d['doctors'] = doctors 
        d['doctors_by_clinic'] = doctors 
        return render_to_response('meddy1/doclistings.html',d) 
    
+0

謝謝你這麼多的精心答案。我非常感謝你的幫助! – 2014-09-12 11:07:39

2

action雲在form元件,而不是輸入端。