简体   繁体   English

不允许的方法(POST):/items/

[英]Method Not Allowed (POST): /items/

So i'm trying to do a filter with django, to filter the items on the main page.所以我正在尝试使用 django 进行过滤,以过滤主页上的项目。 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 :这是我在views.py中的 function :

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 :这是来自我的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.当我使用此表单并单击提交按钮时,屏幕变白,在我的控制台中,出现错误Method Not Allowed (POST): /items/ Has this something to do with the fact i'm using the generic.ListView for my view?这与我使用generic.ListView的事实有关吗?

Thank you谢谢

in list view is POST not allowed.在列表视图中不允许 POST。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM