0

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
Angelica_92-
  • 37
  • 1
  • 1
  • 3
  • 2
    Please don't post your code as image. Edit your question and include relevant parts of code as text. Read more about [mcve](https://stackoverflow.com/help/mcve) – Aleks Andreev Oct 17 '17 at 08:29
  • 1
    It looks like you were trying to issue `PUT` and `DELETE` requests from a simple HTML form. [That won't work](https://stackoverflow.com/questions/5162960/should-put-and-delete-be-used-in-forms) without some JavaScript/jQuery magic. – DaSourcerer Oct 17 '17 at 08:32
  • How can I use Jquery to fix it? Could you please give me an example :) – Angelica_92- Oct 17 '17 at 08:41
  • Sure, [this answer](https://stackoverflow.com/a/1200312/3012385) should have everything you need. – DaSourcerer Oct 17 '17 at 08:47

0 Answers0