Python,Flask方法不允许

时间:2015-03-30 17:57:12

标签: python flask

我正在学习python,我正在尝试编写应用程序。我完成了基础知识。我遵循了一个有用的教程但却陷入困境。我的理解是100%达到划痕,所以任何帮助和推理背后都会很棒。

我在尝试提交表单时遇到方法不允许错误。我将在下面发布代码,希望有人可以提供帮助。

new_action.py

{% extends "base.html" %}

{% block content %}
    <h2>New Action Request</h2>
    {% include 'flash.html' %}
    <div class="well">
       <form class="form-horizontal" action="" method="post" name="post">
            {{ form.hidden_tag() }}
            <div class="control-group{% if form.errors %} error{% endif %}">
                <label class="pull-right" for="post">Date: {{
                    datetime.date(datetime.utcnow()) }}</label>

                <div class="controls">
                    {{form.timestamp}}
                </div>



                <label class="control-label" for="post">Raised By:</label>

                <div class="controls">
                    {{ form.raised_by }}
                    {% for error in form.errors.post %}
                        <span class="help-inline">[{{ error }}]</span><br>
                    {% endfor %}
                </div>
                <br/>

                <label class="control-label" for="post">Source:</label>

                <div class="controls">
                    {{ form.source }}
                    {% for error in form.errors.post %}
                        <span class="help-inline">[{{ error }}]</span><br>
                    {% endfor %}
                </div>
                <br/>

                <label class="control-label" for="post">Category:</label>
                <div id="radios1" class="controls" data-toggle="buttons-radio">
                    <button type="button" class="btn active" name="health_safety" value="health_safety">Health &
                        Safety</button>
                    <button type="button" class="btn" name="quality" value="quality">Quality</button>
                    <input type="hidden" name="category" value={{request.form['category']}} />
                </div><br/>
                <br/>

                <label class="control-label" for="post">Sub-Category:</label>

                <div class="controls">
                    {{ form.sub_category }}
                    {% for error in form.errors.post %}
                        <span class="help-inline">[{{ error }}]</span><br>
                    {% endfor %}

                </div>
                <br/>

                <label class="control-label" for="post" width="80%" >Description:</label>

                <div class="controls" >
                    {{ form.issue }}

                    {% for error in form.errors.post %}
                        <span class="help-inline">[{{ error }}]</span><br>
                    {% endfor %}
                </div>
                <br/>

                <label class="control-label" for="post">Immediate Action:</label>

                <div class="controls">
                    {{ form.immediate_action }}

                    {% for error in form.errors.post %}
                        <span class="help-inline">[{{ error }}]</span><br>
                    {% endfor %}
                </div>

            </div>
            <div class="control-group">
                <div class="controls">
                    <input class="btn btn-primary" type="submit" value="Submit Action Request">
                </div>
            </div>
        </form>
    </div>
{% endblock %}

Views.py

@app.route('/new_action', methods=['GET', 'SET'])
@login_required
def new_action():
    form = ActionRequestForm()
    if request.method == 'POST':
        return redirect(url_for('index'))
        #actionrequest = ActionRequest(id = form.id.data, category = form.category.data)


        '''post = Post(body=form.post.data, timestamp=datetime.utcnow(),
                author=g.user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')'''

    return render_template('new_action.html',
                           user = user,
                           form = form,
                           datetime = datetime
                            )

2 个答案:

答案 0 :(得分:5)

您的表单正在尝试将数据发布到服务器。这是一个HTTP POST请求。您可以在视图中定义GET和SET方法。你需要在那里使用POST。

@app.route('/new_action', methods=['GET', 'POST'])  # Changed SET to POST here
@login_required
def new_action():
    # ... what ever...

您应该通过此RFC获取HTTP。没有SET方法。

答案 1 :(得分:1)

@app.route('/new_action', methods=['GET', 'SET'])

这行代码只允许&#34; GET&#34;和&#34; SET&#34;方法,而你正试图&#34; POST&#34;到那条路。