【问题标题】:"Method Not Allowed The method is not allowed for the requested URL."“方法不允许 请求的 URL 不允许该方法。”
【发布时间】:2016-12-12 21:36:13
【问题描述】:

我已经阅读了这个问题的相关帖子,但没有找到一个可以修复或似乎匹配的答案(如果我错过了它,我深表歉意,我浏览了 10 个帖子)。

编写一个在数据库中查找条目的搜索页面。最初我把它写成两个独立的函数。一是显示搜索框,二是进行实际搜索并返回结果。这很好用,但我试图通过将搜索框保持在页面顶部并仅返回结果(如果有)来使其更加“用户友好”。

似乎是一件简单的事情,但行不通。

views.app 中的 Python 代码

@app.route('/search', methods=['POST'])
def SearchForm():

    if request.method == "POST":
        output = []
        searchterm = request.form['lookingfor']
        whichName = request.form['name']
        if searchterm:
            conn = openDB()
            results = findClient(conn, searchterm, whichName)
            for r in results:
                output.append({'id': r[0], 'fname': r[1], 'lname': r[2], 'phonen': r[3], 'email': r[4], 'started': r[5],
                               'opt': r[6], 'signup': r[7], 'enddate': findEndDate(r[7], r[5])})
            closeDB(conn)
            if output:
                message = "Record(s) Found"
            else:
                message = "Nothing found, sorry."
            return render_template('search.html', message=message, output=output)
        else:
            output = []
            message = "Please enter a name in the search box"
            return render_template('search.html', message=message, output=output)
    else:
        return render_template('search.html')

用于 search.html 的 HTML

{% extends "baseadmin.html" %}
{% block content %}
<div>
    <form action="{{url_for('search')}}" method="post">
        <p>Search for a Client: <input type="text" name="lookingfor"/></p>
        <input type="radio" name="name" value="fname" id="fname"><label for="fname">First Name</label>
        <input type="radio" name="name" value="lname" id="lname"><label for="lname">Last Name</label>
        <input type="submit" value="submit"/>
    </form>
</div>
<h2>{{ message }}</h2>
<div>
    <table>
      <tr>
        <th>Name</th>
        <th>Email Address</th>
        <th>Phone Number</th>
        <th>Trial Method</th>
        <th>Start Date</th>
        <th>EndDate</th>
      </tr>
      {% for client in output %}
        <tr>
          <td>{{ client['fname'] }} {{ client['lname'] }}</td>
          <td>{{ client['email'] }}</td>
          <td>{{ client['phonen'] }}</td>
          <td>{{ client['started'] }}</td>
          <td>{{ client['signup'] }}</td>
          <td>{{ client['enddate'] }}</td>
        </tr>
      {% endfor %}
    </table>
</div>
{% endblock %}

【问题讨论】:

  • 您是否发出 GET 请求来加载初始页面?是这样,那是你的问题。将 GET 添加到允许的方法中。
  • @dirn - 修复了它。谢谢你。但我不明白为什么我需要 GET 方法。我没有在搜索页面的 url 中传递任何变量,也没有寻找任何变量。使用 http://.com/search 访问页面
  • 当您的浏览器通过表单提交以外的方法请求页面时,会发出 GET 请求。 POST 只能通过表单提交和 JavaScript 请求进行。
  • 啊哈!因此,如果我理解正确,除非定义,否则默认情况下方法是 GET。完全有道理,现在看起来很明显。感谢您的帮助和教育。

标签: python methods flask http-status-code-405


【解决方案1】:

正如@dirn 在他的comment 中已经提到的,@app.route('/search', methods=['POST']) 中的methods=['POST'] 表示函数SearchForm 和URL '/search' 将只接受POST 请求。如果有人尝试仅使用 URL 访问该页面,他们将使用 GET 请求进行访问。

将行更改为@app.route('/search', methods=['GET', 'POST']) 应该可以修复错误。

(主要回答(1)显示完整的解决方案和(2)使solution provided by @dirn可见。)

【讨论】:

    猜你喜欢
    • 2017-01-10
    • 2014-01-08
    • 2020-07-30
    • 2021-08-16
    • 2016-04-23
    • 2023-03-09
    • 1970-01-01
    • 2021-11-13
    • 2020-03-05
    相关资源
    最近更新 更多