【问题标题】:Flask POST form error "Method Not Allowed"烧瓶 POST 表单错误“方法不允许”
【发布时间】:2020-04-24 09:54:51
【问题描述】:

我正在尝试将文本输入表单中的一个参数传递到我的 app.py 中,但我收到以下错误消息:

方法不允许 请求的 URL 不允许该方法。

这里是我的 app.py 配置:

@main_blueprint.route('/reports/daily_reports') 
def downloadsss():
    if request.method == 'GET':
    daily_path = "./app/templates/Repo/DailyReports"
    daily_listOfFiles = os.listdir(daily_path)
    return render_template('main/DailyReports.html', len = len(daily_listOfFiles), daily_listOfFiles 
    = daily_listOfFiles)
    elif request.method == 'POST' and 'download' in request.form:
    download = request.form.get('download')
    path = 'C:/Users/Ahmed Mustafa/FlaskProject/app/templates/Repo/DailyReports/' + download
    return send_file(path, as_attachment=True)

以下是我的 HTML 代码:

{% extends "main/main_base.html" %}  {# main/main_base.html extends layout.html #}
{% block content %}
<head>
<style>
 body {background-color: powderblue;}
 h1   {color: blue;}
 p    {color: red;}
 h2   {color: black; font-size: 20px;  font-family: "Lucida Sans Unicode", "Lucida Grande", sans- 
 serif;}
 th   {vertical-align: top; align-items: center;}
 table{align-items: center; }
 </style>
 <div class="jumbotron text-center">
    <p><font size="6">This page is to show the Daily Reports list!</font></p>

<form method="post">
<input type="text" name="download" />
<input type="submit" value="Download" />
</form>

    <!-- For loop logic of jinja template -->
        <li class="list-group-item"><font size="6">
        {% print('Total Number of Reports:'), len %}</font></li>
        <div align="left" >         
        {%for i in range(0, len)%}
        {% print('Report Number:'),[i+1] %}
        <li class="list-group-item">
        <a href="file:///C:/Users/Ahmed 
 Mustafa/FlaskProject/app/templates/static/DailyReports/{{daily_listOfFiles[i]}}"> 
 {{daily_listOfFiles[i]}}</a>
        <p>&nbsp;</p>
        <h2></h2>
        </li> 
        {% endfor %}
    </div>
 </div>
{% endblock %}

【问题讨论】:

    标签: python html forms flask post


    【解决方案1】:

    您需要通过将 POST 添加到路由的方法参数来允许路由上的 POST 请求:

    @main_blueprint.route('/reports/daily_reports', methods=('GET', 'POST'))
    

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多