0

I have a HTML drop list with certain values that I want to pass to my generic view in order to return a filtered list.

The class:

class filteredListView(generic.ListView):
    template_name = 'guestlist/guestlist.html'

    def get_queryset(self, **kwargs):
        relation = kwargs.get('relation', None)
        if relation == 'all':
            return Guest.objects.all()
        if relation == 'grooms_side':
            return Guest.objects.filter(grooms_side=True)
        if relation == 'brides_side':
            return Guest.objects.filter(brides_side=True)
        if relation == 'friends':
            return Guest.objects.filter(friends=True)

html block:

<th colspan="4">
  <label>Filter by Relation
    <select onchange="location='filtered'">
      <option value="all">All</option>
      <option value="grooms_side">Groom's Side</option>
      <option value="brides_side">Brides's Side</option>
      <option value="friends">Friends</option>
    </select>
  </label>
</th>

I've tried passing the value with a normal href like a regular view but that gave me a NoReverseMatch exception.

urls.py:

from django.conf.urls import url
from . import views

app_name = 'guestlist'

urlpatterns = [

    # /guestlist/
    url(r'^$', views.indexView.as_view(), name='index'),
    # /guestlist/
    url(r'^guestlist/$', views.guestListView.as_view(), name='guestlist'),
    # /guestlist/add
    url(r'^guestlist/add/$', views.guestCreate.as_view(), name='add'),
    # /guestlist/filtered
    url(r'^guestlist/filtered$', views.filteredListView.as_view(), {'relation': 'relation'}, name='filtered'),

]

My question is how do i pass the value from the drop list's options to the view. Thanks.

Edit: I've changed a few things according to one of the answers but the question still stands. What do I put in the dict in urls.py in order to pass the value?

mrpink121
  • 191
  • 1
  • 3
  • 13
  • See [here](https://docs.djangoproject.com/en/1.11/topics/http/urls/#passing-extra-options-to-view-functions) in the documentation – Chiheb Nexus Aug 06 '17 at 06:44
  • Also, this [answer](https://stackoverflow.com/a/30200235/3926995) – Chiheb Nexus Aug 06 '17 at 06:47
  • Also, this example shows how to pass a hardcoded string, where I look to pass a variable. – mrpink121 Aug 06 '17 at 07:06
  • @ChihebNexus question has been edited according to your answer, so thank you for getting me a bit closer to solution! Still trying to pass a variable and not a hardcoded string to the dict. – mrpink121 Aug 06 '17 at 07:21

1 Answers1

4

You need to use a form to send the value from the dropdown to the filter view. And there is no reason to put anything in the URL pattern itself.

url(r'^guestlist/filtered$', views.filteredListView.as_view(), name='filtered'),


<th colspan="4">
  <form action="{% url filtered %}" method="GET">
  <label>Filter by Relation
    <select name="relation">
      <option value="all">All</option>
      <option value="grooms_side">Groom's Side</option>
      <option value="brides_side">Brides's Side</option>
      <option value="friends">Friends</option>
    </select>
  </label>
  <input type="submit" value="Filter">
  </form>
</th>

and in the view:

def get_queryset(self, **kwargs):
    relation = self.request.GET.get('relation', None)
    ...
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895