I am working on a Django project where I dynamically add new languages via an API. The workflow is as follows:
I am using Rosetta for the static translations and Parler for the DB translations
I have tried several approaches to restart Django automatically via an API:
Issue: When calling the restart API, Django stops successfully but does not restart. The restart only works when I manually stop the server and restart it from the terminal. The script kills the PID but doesn’t properly relaunch Django. What I Need Help With: What is the best way to restart Django from within an API call? Is there a more reliable way to restart Django inside Docker as well? Why does manually stopping Django and then restarting work, but the script approach does not?
Relevant Repo with explaination on how to download the docker + other steps: https://github.com/Yemeni/ai_blog_system/
I have checked other approaches which are dynamic reload of settings.py but it's not recommended as per the django docs
EDIT:
Parler is a good option but it’s support was until 4.0, I need to find something LTS or clone Django Parler
The following link is for the stuff supported in Django 5
https://djangopackages.org/grids/g/i18n/
The following link is for the Parler changelogs (2021):
https://django-parler.readthedocs.io/en/stable/changelog.html
What is the best way to restart Django from within an API call? Is there a more reliable way to restart Django inside Docker as well?
The best way is not to restart. If data is dynamic, it means you typically store and retrieve it from the database.
Restarting a (Django) webserver through an API does not make much sense. It is an evident exploit for a Denial of Service (DoS) attack: you just ask a restart over and over again, and the web server is only responsive for a few milliseconds in between.
It also can cause other vulnerabilities: if a person somehow manages to change a file, a reload means that person can run arbitrary code, which is a severe problem. Usually the idea is to clone the files from a repository you check and immediately run the server with these files, and thus don't reload when a file changes.
According to your use case scenario which is adding/removing languages without DB migrations I suggest adding all the language codes https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes then simply activate/disable the languages depending on the needs.