简体   繁体   中英

Recommended way of accessing BASE_DIR in Django

I am coding an automated menu module in Django that will list all the packages in BASE_DIR that have a urls.py file. For this I obviously need to access BASE_DIR, but I haven't been able to find a standard way of doing this. I could just do:

from myproject import settings

do_stuff(settings.BASE_DIR)

but I'd rather not hardcode it to myproject since this menu module could also be used for other applications. Is there a better solution?

This isn't a question about BASE_DIR, but about importing settings. Your proposed solution is not valid Python in any case: imports don't use file paths like that.

The standard and fully documented way of importing settings in Django is this:

from django.conf import settings

This will always work, in any Django project, and will allow you to access any of the settings.

For example, in a views.py file, we can do the following to use the value of BASE_DIR

from django.conf import settings
value = settings.BASE_DIR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM