最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Restart a Django App via API After Modifying settings.py? - Stack Overflow

programmeradmin4浏览0评论

I am working on a Django project where I dynamically add new languages via an API. The workflow is as follows:

  • Add a new language via the API (http://localhost:8000/core/swagger/).
  • Via API /generate/ Generate the .mo and .po files after adding the language.
  • The new language is added to languages.json, which is loaded into settings.py at startup.
  • To apply the new language, Django needs to be restarted.

I have tried several approaches to restart Django automatically via an API:

  • ✅ Clearing Django's cache before restarting.
  • ✅ Using a .sh script to kill the process and restart Django.
  • ✅ Manually stopping and restarting Django (this works fine manually, but not via the script).

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: /

I have checked other approaches which are dynamic reload of settings.py but it's not recommended as per the django docs

I am working on a Django project where I dynamically add new languages via an API. The workflow is as follows:

  • Add a new language via the API (http://localhost:8000/core/swagger/).
  • Via API /generate/ Generate the .mo and .po files after adding the language.
  • The new language is added to languages.json, which is loaded into settings.py at startup.
  • To apply the new language, Django needs to be restarted.

I have tried several approaches to restart Django automatically via an API:

  • ✅ Clearing Django's cache before restarting.
  • ✅ Using a .sh script to kill the process and restart Django.
  • ✅ Manually stopping and restarting Django (this works fine manually, but not via the script).

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/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

Share Improve this question edited 4 hours ago VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked 11 hours ago Yousef Al-HadhramiYousef Al-Hadhrami 13511 bronze badges 3
  • 4 I hope you can't. You just redeploy it. – willeM_ Van Onsem Commented 11 hours ago
  • 3 I feel that this question is looking at things from a very naive perspective. A production deployment usually has a WSGI server like Gunicorn which passes requests to multiple processes running Django. Adding on top of that some times you have multiple instances of the WSGI server itself (Running on different machines / VMs / containers). This is just one part, you further typically want all code changes to go through version control (Which naturally should include the language files, etc.). As willeM_ Van Onsem mentions the best option is to just redeploy. – Abdul Aziz Barkat Commented 11 hours ago
  • well, the language files (the .mo and .po) are going to be outside of the docker + the db, maybe a cache server or something. as well the langauges.json. so no change on the code will be made the changes are outside of the docker. the language translations are going to be done via a separate api/software/ etc – Yousef Al-Hadhrami Commented 11 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

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.

So I think you should move the problem from .po files, which are used for static translations, to tools like django-parler or django-translated-fields. These move the problem to the database: for example store for a field that needs translation, it makes name, name_en, name_fr, name_ar, and so on, and select the correct field based on the language of the request.

发布评论

评论列表(0)

  1. 暂无评论