2015-03-30 72 views
0

我目前正在學習python,並且正在嘗試編寫一個應用程序。我已經完成了基礎知識。我遵循了一個有用的教程,但卻陷入了困境。我的理解是百分百的,所以任何幫助和推理都很棒。Python,燒瓶方法不允許

我在嘗試提交表單時遇到方法不允許的錯誤。我會發佈下面的代碼,希望有人可以提供幫助。

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 
          ) 

回答

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... 

對於HTTP,您應該通過RFC。沒有SET方法。

+0

感謝您的幫助,讚賞 – 2015-03-31 07:12:12

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

這行代碼是隻允許 「GET」 和 「SET」 的方法,而你正在試圖「POST 「到那條路。

+0

感謝您的幫助,感謝 – 2015-03-31 07:12:55