【问题标题】:Method Not Allowed (POST) - search不允许的方法 (POST) - 搜索
【发布时间】:2018-12-01 16:41:27
【问题描述】:

任务:在 django-table 顶部添加简单搜索以过滤其值。

问题:f^tup 某处并在将值放入搜索窗口后传递请求时收到此错误:

Method Not Allowed (POST): /warscrolls/
[22/Jun/2018 10:27:17] "POST /warscrolls/ HTTP/1.1" 405 0

我的网址是这样的:

url(r'^warscrolls/$', ScrollListView.as_view(filter_class=ScrollListFilter,
        template_name='scroll_list.html'), name='warscrolls'),

我在 html 模板中的搜索表单如下所示:

<form method="post" class="form-inline form-search pull-right">
{% csrf_token %}
<div>
<input id="search_form_id" name="search" type="text" class="form-control col-md-3" placeholder="ID, Name, Account #, ZIP"{% if search %} value="{{ search }}"{% endif %}>
<button type="submit" class="btn btn-small btn-dark"><i class="fa fa-search"></i> Search</button>
 </div>
 </form>

我错过了什么?

【问题讨论】:

    标签: django django-views django-filter django-tables2


    【解决方案1】:

    要启用POST,您的基于类的视图应实现post() 方法。但是 django ListView 类默认没有实现 post() 方法,这会引发错误。

    实际上,使用GET 请求搜索选项是常见的做法,因此要修复错误,您可以像这样简单地更新您的 html 表单:

    <form method="get" class="form-inline form-search pull-right">
    <div>
    <input id="search_form_id" name="search" type="text" class="form-control col-md-3" placeholder="ID, Name, Account #, ZIP"{% if search %} value="{{ search }}"{% endif %}>
    <button type="submit" class="btn btn-small btn-dark"><i class="fa fa-search"></i> Search</button>
    </div>
    </form>
    

    【讨论】:

    • 这很容易。谢谢。
    猜你喜欢
    • 2021-02-10
    • 2022-01-23
    • 1970-01-01
    • 2020-06-17
    • 2014-05-23
    • 1970-01-01
    • 2021-02-09
    • 2020-11-13
    • 2020-06-18
    相关资源
    最近更新 更多