django:使用选择下拉输入实现搜索

时间:2016-06-25 00:44:16

标签: django search drop-down-menu django-forms django-urls

所以这是我的HTML。子类别将根据使用js选择的类别由选项填充。 home.html的:

<form method="GET" action="{% url 'searchapp:search' city category %}"
      class="form-inline row input-group" id="search">
    <select class="form-control " id="place" style="" name="city" title="City" required>
        <option>Choose a City...</option>
        <!--<option value="noida">Noida</option>-->
        <option value="delhi">Delhi/NCR</option>
        <option value="delhi" disabled>Coming Soon...</option>
    </select>

    {% csrf_token %}

    <select class="form-control " id="category" style="" onchange="subcat()"
            title="Category" name="category" required>
        <option value="none">Choose a category</option>
        <option value="aprl">Apparels & Clothing</option>
        <-- more options -->
    </select>

    <select class="form-control " id="ms" style="" name="subcateg" title="Sub-Category"
            required>
        <option value="none">Choose a category first.</option>
        <-- more options -->
    </select>
    <span class="input-group-btn " id="inputbutton">
        <button type="submit" class="btn btn-primary" id="butn">Find Vendors!</button>
    </span>
</form>

这是搜索表单操作的网址。
searchapp / urls.py

app_name = 'searchapp'

urlpatterns = [

    url(r'^(?P<city>[a-z]+)/(?P<category>[a-z]+)$', views.search, name='search'),
    # url(r'^$', views.search, name='search'),
]

homeapp / urls.py

app_name = 'home'

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

现在这里是我要显示所选城市和类别的视图

searchapp / views.py

def search(request, city, category):
    # return render(request, 'homeapp/index.html')
    return HttpResponse('<h3>search detail for :'+str(city)+str(category)+'</h3>')

homeapp / views.py:

# Create your views here.
def index(request):
    return render(request, 'homeapp/index.html')

当我运行本地服务器并转到我的主页时,应显示该表单,但我得到此错误

Error

错误是:

NoReverseMatch at /
Reverse for 'search' with arguments '('', '')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['search/(?P<city>[a-z]+)/(?P<category>[a-z]+)$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.10a1
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'search' with arguments '('', '')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['search/(?P<city>[a-z]+)/(?P<category>[a-z]+)$']
Exception Location: C:\Users\singhsterabhi\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-1.10a1-py3.5.egg\django\urls\resolvers.py in _reverse_with_prefix, line 374
Python Executable:  C:\Users\singhsterabhi\AppData\Local\Programs\Python\Python35-32\python.exe
Python Version: 3.5.1
Python Path:    
['E:\\projects\\web\\user-portal',
 'C:\\Users\\singhsterabhi\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\django-1.10a1-py3.5.egg',
 'C:\\Users\\singhsterabhi\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\psycopg2-2.6.1-py3.5-win32.egg',
 'E:\\projects\\web\\user-portal',
 'C:\\Users\\singhsterabhi\\AppData\\Local\\Programs\\Python\\Python35-32\\python35.zip',
 'C:\\Users\\singhsterabhi\\AppData\\Local\\Programs\\Python\\Python35-32\\DLLs',
 'C:\\Users\\singhsterabhi\\AppData\\Local\\Programs\\Python\\Python35-32\\lib',
 'C:\\Users\\singhsterabhi\\AppData\\Local\\Programs\\Python\\Python35-32',
 'C:\\Users\\singhsterabhi\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages']
Server time:    Sat, 25 Jun 2016 05:48:08 +0530

请告诉我如何解决它?

0 个答案:

没有答案