[英]How use django with decouple
I am trying to use python-decouple for sensitive data in my project but When i use decouple.config for SECRET_KEY it raises an error我试图在我的项目中使用 python-decouple 处理敏感数据,但是当我将 decouple.config 用于 SECRET_KEY 时,它会引发错误
error错误
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/admin/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/admin/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/admin/Legaland/src/sqh/settings/dev.py", line 1, in <module>
from ._base import *
File "/home/admin/Legaland/src/sqh/settings/_base.py", line 40, in <module>
SECRET_KEY = config("SECRET_KEY")
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 199, in __call__
return self.config(*args, **kwargs)
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 83, in __call__
return self.get(*args, **kwargs)
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 65, in get
value = self.repository[option]
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 113, in __getitem__
return self.parser.get(self.SECTION, key)
File "/usr/lib/python3.6/configparser.py", line 800, in get
d)
File "/usr/lib/python3.6/configparser.py", line 394, in before_get
self._interpolate_some(parser, option, L, value, section, defaults, 1)
File "/usr/lib/python3.6/configparser.py", line 444, in _interpolate_some
"found: %r" % (rest,))
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%^e=(r2l0*73)4zxv!(!4x(%(_koxr049zlesn3"'
How shall i make it read SECRET_KEY as text我该如何让它将 SECRET_KEY 作为文本读取
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config("SECRET_KEY")
I am already I using .ini file for my email address and password the problem is that it tries to perform SECRET_KEY instead of accepting it as a raw text how can i prevent it我已经使用 .ini 文件作为我的电子邮件地址和密码,问题是它尝试执行 SECRET_KEY 而不是将其作为原始文本接受我如何防止它
you should have a env file like below你应该有一个像下面这样的 env 文件
.env .env
SECRET_KEY='aaassskkkk'
and then source your .env file using below command然后使用以下命令获取 .env 文件
source .env
then try the below code it will works然后尝试下面的代码它会起作用
from decouple import config
SECRET_KEY = config('SECRET_KEY', '')
if the SECRET_KEY not found in env it will take as ''如果在 env 中找不到 SECRET_KEY 它将作为 ''
Decouple supports .ini
and .env
files.解耦支持
.ini
和.env
文件。 For use .env file simply create a .env
text file in your repository's root directory.要使用 .env 文件,只需在存储库的根目录中创建一个
.env
文本文件。
SECRET_KEY='super-secret-key'
you can also override the parameter您还可以覆盖参数
SECRET_KEY='other-secret-key' python manage.py runserver
Right to the point, as said by Henrique Bastos, here :切中要害,由恩里克巴斯托斯说, 在这里:
Note: Since ConfigParser supports string interpolation, to represent the character % you need to escape it as %%.
注意:由于 ConfigParser 支持字符串插值,为了表示字符 %,您需要将其转义为 %%。
To use python-decouple with a .ini file you can do this: Use your variable like said in the first answer:要将 python-decouple 与.ini文件一起使用,您可以执行以下操作:使用您的变量,如第一个答案中所述:
SECRET_KEY = config(SECRET_KEY, default='some-random-text')
You can use cast, if needed, like:如果需要,您可以使用强制转换,例如:
DEBUG = config(DEBUG, default=True, cast=bool)# in the .ini file, the DEBUG value can be: 0, 1, True, False...
So for last, your traceback error show that there is a percent sign in your .ini string value, you may need to do a scape with a %% as said in the official documentation https://github.com/henriquebastos/python-decouple所以最后,你的回溯错误表明你的 .ini 字符串值中有一个百分号,你可能需要像官方文档https://github.com/henriquebastos/python- 中所说的那样用 %% 做一个 scape-解耦
Do a escape parser with double percents, instead just one and the error will be gone!做一个带有双百分比的转义解析器,而不是只有一个,错误就会消失!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.