Need some help explaining how to update and delete data with PUT and DELETE request using the Flask framework. Currently I cannot update or delete data, and get the 404 error message (know that it means Not Found) My data is saved in a JSON file and I´m using templates to display the data. Appreciate the help a lot!
My code: 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