Commits

Luke Plant  committed e104443

Added view to demo project that does combines a text search with a FilterSet

  • Participants
  • Parent commits 6a25de2

Comments (0)

Files changed (4)

File django_easyfilters/tests/templates/book_search.html

+{% extends "base.html" %}
+
+{% block content %}
+
+<form action="" method="GET">
+Search <input name="search_q" type="text" value="{{ request.GET.search_q }}"><input type="submit">
+{% for k, v in request.GET.items %}
+  {% if k != "search_q" %}
+    <input type="hidden" name="{{ k }}" value="{{ v }}">
+  {% endif %}
+{% endfor %}
+</form>
+
+{{ booksfilter }}
+
+<p>The following books were found:</p>
+{% for book in books %}
+<div class="book">
+  <h2>{{ book.name }}</h2>
+  <div><b>Authors:</b>
+    {% for author in book.authors.all %}
+    {{ author }}{% if not forloop.last %},{% endif %}
+    {% endfor %}
+    <br><b>Genre:</b> {{ book.genre }}</div>
+</div>
+{% endfor %}
+
+
+{% endblock %}

File django_easyfilters/tests/urls.py

 
 urlpatterns = patterns('',
                        (r'^books/', 'django_easyfilters.tests.views.books'),
+                       (r'^book-search/', 'django_easyfilters.tests.views.book_search'),
                        (r'^authors/', 'django_easyfilters.tests.views.authors'),
                        (r'^admin/', include(admin.site.urls)),
 

File django_easyfilters/tests/views.py

                                             'title': "Authors",
                                             })
 
+
+def book_search(request):
+    books = Book.objects.all()
+    if 'search_q' in request.GET:
+        books = books.filter(name__icontains=request.GET['search_q'])
+
+    booksfilter = BookFilterSet(books, request.GET)
+    return render(request, "book_search.html", {'books': booksfilter.qs,
+                                                'booksfilter': booksfilter,
+                                                'title': "Books",
+                                                })

File test_settings.py

     'django_easyfilters.tests',
 ]
 
+TEMPLATE_CONTEXT_PROCESSORS = [
+    "django.contrib.auth.context_processors.auth",
+    "django.core.context_processors.debug",
+    "django.core.context_processors.i18n",
+    "django.core.context_processors.media",
+    "django.core.context_processors.static",
+    "django.core.context_processors.tz",
+    "django.contrib.messages.context_processors.messages",
+    "django.core.context_processors.request",
+]
+
 ROOT_URLCONF = 'django_easyfilters.tests.urls'
 
 DEBUG = True