I am currently working on functionality where users wants to keep a single message in header of a page to notify others of something happening in system. Essentially a header that can be updated by any user.
As this header needs to be shown to every user in site I can't store it in local storage or user's table. Also this is a single line of message on a single page, so making a whole database table for this sounds utterly stupid. So to achieve this functionality for now I have added a json file in BE which just have this message in there and a view which allows users to edit this file. I have also placed this file in gitignore because data change in this should not reflect in git.
This system works fine for now, but when I redeploy using Docker this file is removed and rebuilt losing the initial message. Is there a better way to store and update this data? Or any way to not lose my data on redeploy?
userdata.json
{
message: "Holiday tomorrow, please report any issues by 3."
}
Reading data in Django View
try:
if not os.path.exists(USERDATA_FILE):
# Create the file with default structure if it doesn't exist
with open(USERDATA_FILE, "w") as file:
json.dump({"message": "Hello World!"}, file, indent=4)
with open(USERDATA_FILE, "r") as file:
data = json.load(file)
except Exception as e:
print("Error in reading data")