I have this program that shows articles and I want to add an option to remove articles. The bottom code is the option to either add or edit an already existing articles content. I was wondering if using part of the code to see if a title exists to remove an article from the list. The json list contains of two objects: title and content. So when removing a title I also want to delete its content.
import json
from bottle import route, run, template, request, redirect, static_file, error
@route('/remove')
articles = read_articles_from_file()
title = getattr(request.forms, "title")
append_flag = True
for i, article in enumerate(articles): #loop through existing articles
if title == article['title']: #if title exists in articles
@route('/update', method="POST")
def update_article():
"""
Receives page title and contents from a form, and creates/updates a
text file for that page.
"""
title = getattr(request.forms, "title")
content = getattr(request.forms, "content")
articles = read_articles_from_file()
append_flag = True
for i, article in enumerate(articles): #loop through existing articles
if title == article['title']: #if new title already exists
articles[i]['content'] = content #update articles content
append_flag = False #update flag to false
break #if title is unique, break loop
if append_flag: #add new post
articles.append({
"title": title.strip(),
"content": content
})
my_file = open("storage/articles.json", "w")
my_file.write(json.dumps(articles, indent=3))
my_file.close()