简体   繁体   中英

Django. Using .ini files in code folder

I wanted to have a .py file using a ConfigParser class to recover information from a .ini file (containing URLs for a 3rd party REST service) Until now this worked since it was finding the file, but now it's not. App structure is:

app
    folder
        file.py
        urls.ini

I assume this is due to my static files settings, which are as following:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

This is because I wanted to mantain settings that were compatible for both local development and web production escenarios (for which I could probably test with code in the web) for Heroku. I wanted to make it so I didn't have to change much stuff.

Thanks in advance.

If you're trying to read some files, you have to get the route.

If you have your static folder in your Django folder it could be easy to get your .ini file doing this

ini = open("%s/folder/yourfile.ini" % BASE_DIR, "r")

And then read your file lines.

In my opinion using a .py file for configuration it's easier than .ini file, and you can have many of them and load depending of a settings.py parameter for example.

Edit: for using Django config file variables

from django.conf import settings
BASE_DIR = 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