把请求python烧瓶

时间:2018-03-04 07:39:42

标签: python json flask put

我正在处理PUT请求,以便能够使用Flask和Python修改我的JSON文件中的数据。问题是它不会保存所做的更改。

以下是我的代码:

@app.route('/updated', methods = ['POST', 'PUT' 'GET'])
def update():
    try:
        title = request.form['title']
        print title

        if request.method == 'POST':
            with open("articles.json", 'r+') as json_File:
                articles = json.load(json_File)
                for article in articles['article']:
                    if title == article['title']:
                        print article['title']
                        print article['author']
                        print article['article_id']
                        article['title'] = title
                        article['author'] = request.form['author']
                        article['text'] = request.form['text']
                        article['article_id'] = request.form['article_id']
                        print article
                        save_article = json.dumps(article, json_File)
                    else:
                        print "article could not be added"
                #json_File.close()
                return render_template('updated.html', save_article = save_article, article = article)

    except:
        print "This didn't work."
        return render_template('errorHandler.html'), 404

3 个答案:

答案 0 :(得分:0)

我认为你应该改变这一部分:

if request.method == 'POST' or request.method == 'PUT':

为了更好的做法,我认为你应该这样做:

if request.method == 'POST' or request.method == 'PUT':
     # do your code here, which edit into your database
if request.method == 'GET':
     # do GET code here, which return data from your database

或者将您的https方法分成不同的功能

答案 1 :(得分:0)

http://blog.luisrei.com/articles/flaskrest.html

的例子
@app.route('/echo', methods = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'])
def api_echo():
    if request.method == 'GET':
        return "ECHO: GET\n"

    elif request.method == 'POST':
        return "ECHO: POST\n"

    elif request.method == 'PATCH':
        return "ECHO: PACTH\n"

    elif request.method == 'PUT':
        return "ECHO: PUT\n"

    elif request.method == 'DELETE':
        return "ECHO: DELETE"

可能最好在装饰器中为每个方法设置if / elif / else,以防止奇怪的bug和边缘情况。

答案 2 :(得分:0)

首先,json.dumps()"转储"到字符串,而不是文件。所以

save_article = json.dumps(article, json_File)

将返回一个字符串,然后绑定到save_article变量,但实际上并未修改该文件。您可能打算使用json.dump(article, json_File)接受文件作为第二个参数。

注意:在Python 2中默认忽略file参数,我假设你使用它,因为它会在Python 3中显示为错误。

可能还有其他问题。一个是文章将附加到文件中,但似乎代码的意图是更新现有文章。在适当的位置更新文本文件通常是不切实际的。更好的方法是迭代文章,更新与标题匹配的文章。然后在最后重写整个文件一次。这是一个例子:

        with open("articles.json", 'r') as json_File:
            articles = json.load(json_File)

        # update any matching articles
        for article in articles['article']:
            if title == article['title']:
                article['author'] = request.form['author']
                article['text'] = request.form['text']
                article['article_id'] = request.form['article_id']

        # rewrite the whole JSON file with updated dictionary
        with open("articles.json", 'w') as json_File:
            json.dump(articles, json_File)

在更新文章数据时,您可能需要考虑使用简单的数据库来管理它。你可以看看Flask SQLAlchemy