So i'm trying to do a filter with django, to filter the items on the main page. A little context:
An item can be assigned to a user or not. I want a filter, to see what items are assigned and which items aren't.
This is my function in views.py
:
class ItemsView(ListView):
template_name = 'inventory/items.html'
context_object_name = 'items_list'
def get_queryset(self):
if (self.request.user.is_authenticated):
if self.request.method == "get":
searched = self.request.GET.get('searched')
if searched == "None":
return Item.objects.filter(assigned_user__isnull=True, company=getCompany(self.request.user))
else:
return Item.objects.filter(assigned_user__isnull=False, company=getCompany(self.request.user))
else:
return Item.objects.filter(company=getCompany(self.request.user))
And this is from my items.html
:
<form method="post" action="/items">
{% csrf_token %}
<select name="item_filter">
<option value="None">Not Checked In</option>
<option value="Checked_In">Checked In</option>
</select>
<input type="submit" value="filter">
</form>
So basically what i want is, that the user can pick one of the two choices in the dropdown-menu, and the items should be listed based on the choice he made.
When i use this form and click the submit button, the screen gets white and in my console, the error
Method Not Allowed (POST): /items/
appears. Has this something to do with the fact i'm using the generic.ListView
for my view?
Thank you
CodePudding user response:
in list view is POST not allowed. But you dont need it: In template:
<form action="/items">
<select name="item_filter">
<option value>Not Checked In</option>
<option value="Checked_In">Checked In</option>
</select>
<input type="submit" value="filter">
</form>
in view:
class ItemsView(ListView):
... # any staff
def get_queryset(self):
query = {'company':getCompany(self.request.user)}
if (self.request.user.is_authenticated):
query['assigned_user__isnull'] = bool(self.request.GET.get('item_filter'))
return Item.objects.filter(query)