简体   繁体   中英

importing settings file in reusable django app

I am creating a reusable django application which can be used in any django project, and storing some credentials in the settings.py file.

settings.py

# Django settings for mytestapp project.
...
...
...
ADMIN_USERNAME='someone'
ADMIN_PASSWORD='someone'

I want to use these credentials in my app. How can I import them ?

I have tried,

import settings.ADMIN_USERNAME

and

from django.conf import settings.ADMIN_USERNAME

But both are not working.

I tried to import these variables from eclipse using ctrl+1 , then these variables imported as,

from mytestapp.settings import ADMIN_USERNAME

How can I import these variables, if I don't know project name (in case mytestapp ) ?

from django.conf import settings

然后你可以访问你的常量,比如settings.ADMIN_USERNAME

Read the documentation . Don't try to import a setting by name; you should just import the settings :

from django.conf import settings

and refer to your specific settings as attributes:

settings.ADMIN_USERNAME

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