如何在flask(python)中使用PUT和DELETE请求?

时间:2017-10-17 08:26:52

标签: python http flask put

需要一些帮助来解释如何使用Flask框架使用PUT和DELETE请求更新和删除数据。目前我无法更新或删除数据,并收到404错误消息(知道它意味着未找到) 我的数据保存在JSON文件中,我使用模板显示数据。非常感谢帮助!

我的代码: PICTURE Put and delete request in code PICTURE Form for put request

@app.route('/updated', methods = ['POST', 'GET'])
def update():
'''Updates article information'''

title = request.form['title']
author = request.form['author']
text = request.form['text']
article_id = request.form['article_id']

print title
article = {'id': article_id, 'title' : title, 'author' : author, 'text' : text}
print article['id']
article[article_id] = article
try:
    with open("articles.json", "w") as json_File:
        print article
        update_article = json.dump(article, json_File, sort_keys=True, indent=4)
        return render_template('updated.html', title = title, author = author, text = text, article_id = article_id)
        json_File.close()
except:
    return render_template('errorHandler.html'), 404

   @app.route('/delete/<int:article_id>', methods = ['POST'])
   def delete_article(article_id):
    '''Deletes article'''
    print article_id
    try:
        with open("articles.json", 'r') as article_file:
            data = json.load(article_file)
            print data
            if article_id == data['id']:
                print 'hej'
            # data.pop(article_id, None)
        return render_template('updated.html', article_id = article_id)

    except:
        return render_template('errorHandler.html'), 404

0 个答案:

没有答案