400 Bad request Error ..... Flask python 2.7

时间:2016-06-13 05:04:45

标签: python flask

我正在尝试执行一个简单的python函数,通过它我想通过引导模式窗口动态更新数据库。无法识别那里的任何错误......再次发生同样的错误......请帮助我...

from flask import Flask, render_template, redirect, json, request,session, url_for, jsonify
from flask.ext.mysql import MySQL
from werkzeug import generate_password_hash, check_password_hash

@app.route('/updatepost', methods = ['POST', 'GET'])
def updatepost():
        try:
                if session.get('user'):
                        _posttitle = request.form['postt']
                        _postcontent = request.form['postd']
                        _postiid = request.args.get('id')
                        con = mysql.connect()
                        cursor = con.cursor()
                        print 555
                        #cursor.execute("UPDATE addpost SET post_title= 'apple' , post_content ='a fruit' WHERE Id = '" + _postiid + "'")
                        cursor.execute("UPDATE addpost SET post_title='" + str(_posttitle) + "', post_content ='" + str(_postcontent) + "' WHERE Id = '" + str(_postiid) + "'")

                        con.commit()

                        return redirect('/userhome')
                        cursor.close()
                        con.close()
                else:
                        return redirect('/')
        except Exception as e:
                return render_template('error.html', error = str(e))

我的HTML代码..

<div class="modal fade" id="editModal" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;
        </span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="editModalLabel">Edit Update</h4>
      </div>
      <div class="modal-body">
        <form role="form" method= POST>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Title:</label>
            <textarea class="form-control" name="postt" id="editTitle"></textarea>
          </div>
          <div class="form-group">
            <label for="message-text" class="control-label">Description:</label>
            <textarea class="form-control" name="postd" id="editDescription"></textarea>
          </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button id="btnUpdate" name = "abc" type="button" onclick = "proceedupdate()" class="btn btn-primary">Update</button>
      </div>
      </form>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

您似乎没有在updatepost()下处理您的request.post:

@app.route('/something', methods=['GET', 'POST'])
def something():
    """
    form, retrieves variables when using POST
    """
    try:

        if request.method == 'POST':
            #do something
        else:
            #do something else
    except Exception as e:
        print(e)

因为在你的html中你是在表单子目录上指定post方法:

form role="form" method= POST>

你应该在def updatepost()中处理“POST”:目前它只处理GET请求,如果请求方法是POST,则不执行任何操作。