Django: get the path to the current django application

I have a Django application with some management commands. In one of these commands I need to write a log file which is located in / logs allong side views.py, models.py, etc.

Given that my script management command is in /management/commands/mycommand.py, is there any reliable way to determine the location of the appdir from mycommand.py?

PS: reliable, I mean without use os.path.abspath( __file__ ), because the location of the file may change later.

+3
source share
1 answer

Why do you need your settings.

Add this to your settings

MY_APP_LOG_DIRECTORY = "path/to/logs"

In your view functions use

from django.conf import settings

Now you can use settings.MY_APP_LOG_DIRECTORYwhatever you want.

+3
source

Source: https://habr.com/ru/post/1788032/


All Articles