Reputation: 35
I've made an Application containing List of trainers. My Index View Displays these trainer profiles from the database. I intend to implement a sear bar to filter these results. I am not getting what i'm doing wrong. As soon as i mention the url in action of the search for, it displays reverse match error
url's.py :
#/trainer/
url(r'^$', views.IndexView.as_view(),name='index'),
#/trainer/<trainer_id>/
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(),name='details'),
#/trainer/trainer/add
url(r'trainer/add/$', views.TrainerCreate.as_view(), name='Trainer-add'),
#/trainer/trainer/<album_id>
url(r'trainer/(?P<pk>[0-9]+)/$', views.TrainerUpdate.as_view(), name='Trainer-update'),
#/trainer/trainer/add
url(r'trainer/(?P<pk>[0-9]+)/delete/$', views.TrainerDelete.as_view(), name='Trainer-delete'),
url(r'^search/$', views.search, name='Search'),
views.py
def search(request):
query = request.GET['q']
trainer= Trainer.objects.filter(name__icontains=query)
return render(request,'trainer/index.html', {'trainer': trainer})
search form in my base template
<form class="navbar-form navbar-left" method="get" action="{% url 'trainer:Search' %}">
<div class="form-group">
<input type="text" id="searchBox" class="input-medium search-query" name="q" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
index.py
<table style="width:100%" class="table table-hover">
<tr>
<th>#</th>
<th>Name</th>
<th>Technology</th>
<th>Location</th>
</tr>
{% for trainer in all_trainers %}
<tr>
<td><input type="checkbox" id="trainer{{ forloop.counter }}" name="trainer" value="{{ trainer.id }}"></td>
<td> <a href="{% url 'trainer:details' trainer.id %}"> {{ trainer.name }}</td>
<td>{{ trainer.technology }}</td></a>
<!-- View Details -->
<td><a href="{% url 'trainer:details' trainer.id %}" class="btn btn-primary btn-sm">View Details</a></td>
<td><a href="../media/{{ trainer.trainer_profile }}" class="btn">Download PDF</a></td>
<!-- Delete Album -->
<td>
<form action="{% url 'trainer:Trainer-delete' trainer.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="trainer_id" value="{{ trainer.id }}" />
<button type="submit" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</td>
</tr>
{% endfor %}
</table>
Upvotes: 1
Views: 175
Reputation: 4643
Are you including the urls.py somewhere?
Otherwise you need to add caret ^ at the start of your regex.
url(r'^trainer/search/$', views.search, name='Search'),
Upvotes: 0
Reputation: 174624
You need {% url 'Search' %}
and not {% url 'trainer:Search' %}
; the :
is for when you have namespaced your urls.
Upvotes: 1
Reputation: 27513
change this line
<form class="navbar-form navbar-left" method="get" action="{% url 'trainer:Search' %}">
to
<form class="navbar-form navbar-left" method="get" action="{% url 'Search' %}">
Upvotes: 0