我想在Django中創建一個簡單的搜索表單。以下是我迄今爲止:NoReverseMatch:無法從模板傳遞參數進行查看。
形式:
<form action="{% url 'search_pub' pub_name=pub_name %}" method="get">
Publication name: <input type="text" id="pub_name" name="pub_name" value="herald">
<input type="submit" value="Search">
</form>
urls.py
url(r'^search/$', views.search, name='search'),
url(r'^results/(?P<pub_name>[\w]+)/$', views.search_pub, name='search_pub'),
views.py
def search(request):
return render(request, 'urlapp/search.html')
def search_pub(request, pub_name):
pubs = Publication.objects.all().filter(title__icontains=pub_name)
return render(request, 'app/results.html', {
'publications': pubs
})
models.py
class Publication(models.Model):
title = models.CharField(max_length=30)
我收到以下錯誤,當我打開搜索頁面http://localhost:8000/search/:
NoReverseMatch at /search/
Reverse for 'search_pub' with keyword arguments '{'pub_name': ''}' not found. 1 pattern(s) tried: ['results/(?P<pub_name>[\\w]+)/$']
我驗證了正則表達式是有效使用本網站pythex。
如果我轉到URL:http://localhost:8000/results/herald,我會得到正確的結果。
我錯過了什麼?
價值? – Rohan
是的,我無法弄清楚爲什麼。 – Animesh