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
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
# 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
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 %%.
To use python-decouple with a .ini file you can do this:
Use your variable like said in the first answer:
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
Do a escape parser with double percents, instead just one and the error will be gone!
you should have a env file like below
.env
SECRET_KEY='aaassskkkk'
and then source your .env file using below command
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 ''
Decouple supports .ini and .env files. For use .env file simply create a .env text file in your repository's root directory.
SECRET_KEY='super-secret-key'
you can also override the parameter
SECRET_KEY='other-secret-key' python manage.py runserver
Related
I tried deploying my django/React (using postgreSQL) app using Render. The deploy worked but no data is being returned.
Also, when I try to load seed data or run the server from my directory the following error is thrown:
Traceback (most recent call last):
File "/Users/xxx/xxx/xxx/xxx/manage.py", line 22, in <module>
main()
File "/Users/xxx/xxx/xxx/xxx/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/core/management/__init__.py", line 386, in execute
settings.INSTALLED_APPS
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 87, in __getattr__
self._setup(name)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 74, in _setup
self._wrapped = Settings(settings_module)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 183, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/Cellar/python#3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/xxx/xxx/xxx/xxx/project/settings.py", line 114, in <module>
'default': dj_database_url.parse(os.environ.get('DATABASE_URL'), conn_max_age=600),
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/dj_database_url.py", line 94, in parse
if "?" in path and not url.query:
TypeError: a bytes-like object is required, not 'str'
Here's part of my settings.py:
DATABASES = {
'default': dj_database_url.parse(os.environ.get('DATABASE_URL'), conn_max_age=600),
}
I've also tried:
DATABASE_URL = os.environ.get('DATABASE_URL')
DATABASES = {
'default': dj_database_url.config()
}
But when I run the server, the app loads but has no data (just like the deployed app).
The code block surrounding line 94 in dj_database_url.py is as follows:
# Split query strings from path.
path = url.path[1:]
if "?" in path and not url.query:
path, query = path.split("?", 2)
else:
path, query = path, url.query
query = urlparse.parse_qs(query)
type(os.environ.get('DATABASE_URL')) returns <class 'NoneType'>
and the value returned for os.environ.get('DATABASE_URL') is None
When I was working on a Django project (blog), I had an error(s) while working on the site. Here are the errors I have appeared:
1: When I entered the python command manage.py makemigrations blog(via the console) in the directory C:\mysite\site\miniproject , then there is this:
Traceback (most recent call last):
File "manage.py", line 23, in <module>
main()
File "manage.py", line 19, in main
execute_from_command_line(sys.argv)
File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Program Files\Python36\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Program Files\Python36\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Program Files\Python36\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\mysite\site\miniproject\blog\models.py", line 5, in <module>
class Post(models.Model):
File "C:\mysite\site\miniproject\blog\models.py", line 12, in Post
author = models.ForeignKey(User, related_name='blog_posts')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
Although I did everything according to the instructions on the website https://pocoz .gitbooks.io/django-v-primerah/content/sozdanie-i-primenenie-migracij.html.
I did everything according to plan, I did everything in order and there was such a mistake. And I do not know how to fix it
Updated all the necessary libraries, entered them in manage.ру (which is located in the directory C:\mysite\site\miniproject ) import django, it didn't help
You have declared a ForeignKey somewhere but not provided the on_delete keyword argument.
If you post the BlogPost model, I can give you an exact answer, but you probably want something like:
models.ForeignKey(..., on_delete=models.CASCADE)
To fix the issue add the key word argument to the BlogPost model in blog/models.py
If you debug your error its pretty self-explanatory:
In line 12 File "C:\mysite\site\miniproject\blog\models.py", line 12, in Post
in your Post model you have a field
author = models.ForeignKey(User, related_name='blog_posts')
You need to change that to:
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
I am using Elasticsearch APM in Python to log requests for a FastAPI app.
There is a mechanism to load custom processors by simply specifying the module path to the custom processor function.
My app layout is as follows:
app
├── __init__.py
├── apm_processors.py
├── main.py
└── version.py
My apm_processors.py file has the following function defined:
def ip_processor(client, event):
# process the event
pass
So, in my app, I do:
# other imports
from elasticapm.contrib.starlette import make_apm_client, ElasticAPM
from . import apm_processors
And to set up APM, I call:
apm_config = {
"SERVICE_NAME": config('ELASTIC_APM_SERVICE_NAME', cast=str, default="Server"),
"SECRET_TOKEN": str(config('ELASTIC_APM_SECRET_TOKEN', cast=Secret, default="")),
"SERVER_URL": config('ELASTIC_APM_SERVER_URL', cast=str, default="http://localhost:8200"),
"PROCESSORS": (
"elasticapm.processors.sanitize_http_request_cookies",
"elasticapm.processors.sanitize_http_headers",
"apm_processors.ip_processor"
)
}
apm = make_apm_client(apm_config)
app = FastAPI()
# other app setup
app.add_middleware(ElasticAPM, client=apm)
This throws an error:
ModuleNotFoundError: No module named 'apm_processors'
With the following stacktrace:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/usr/local/lib/python3.9/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.9/site-packages/uvicorn/subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 60, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 67, in serve
config.load()
File "/usr/local/lib/python3.9/site-packages/uvicorn/config.py", line 458, in load
self.loaded_app = import_from_string(self.app)
File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 24, in import_from_string
raise exc from None
File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/statisticsserver/./app/main.py", line 75, in <module>
apm = make_apm_client(apm_config)
File "/usr/local/lib/python3.9/site-packages/elasticapm/contrib/starlette/__init__.py", line 70, in make_apm_client
return client_cls(config, **defaults)
File "/usr/local/lib/python3.9/site-packages/elasticapm/base.py", line 153, in __init__
"processors": self.load_processors(),
File "/usr/local/lib/python3.9/site-packages/elasticapm/base.py", line 640, in load_processors
return [seen.setdefault(path, import_string(path)) for path in processors if path not in seen]
File "/usr/local/lib/python3.9/site-packages/elasticapm/base.py", line 640, in <listcomp>
return [seen.setdefault(path, import_string(path)) for path in processors if path not in seen]
File "/usr/local/lib/python3.9/site-packages/elasticapm/utils/module_import.py", line 47, in import_string
module = import_module(module_path)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'apm_processors'
As far as I can tell it uses import_module to import the function as specified, but for some reason it claims there is no such module — although I am actually able to import it.
How can I specify the processor?
Since the context/main module of the application is app, the import should be done as follows:
"PROCESSORS": (
"elasticapm.processors.sanitize_http_request_cookies",
"elasticapm.processors.sanitize_http_headers",
"app.apm_processors.ip_processor"
)
This way, the import succeeds and the function will be called for each event.
I've tried to configurate pre-commit + mypy + django-stubs, so I have error when i try to commit.
ModuleNotFoundError: No module named 'environ'
My configs:
.pre-commit-config.yaml
- repo: https://github.com/pre-commit/mirrors-mypy
rev: ''
hooks:
- id: mypy
exclude: "[a-zA-Z]*/(migrations)/(.)*"
args: [--config=setup.cfg,
--no-strict-optional,
--ignore-missing-imports]
additional_dependencies: [django-stubs]
setup.cfg
[mypy]
allow_redefinition = True
check_untyped_defs = True
ignore_missing_imports = True
incremental = True
strict_optional = True
show_traceback = True
warn_no_return = False
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
plugins = mypy_django_plugin.main
show_column_numbers = True
[mypy.plugins.django-stubs]
django_settings_module = config.settings.local
[mypy_django_plugin]
ignore_missing_model_attributes = True
[mypy-*.migrations.*]
# Django migrations should not produce any errors:
ignore_errors = True
It's look like mypy ignore option ignore_missing_imports = True.
Does anyone have any ideas?
Full error trace:
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /home/**/.cache/pre-commit/patch1646008720-58913.
[WARNING] The 'rev' field of repo 'https://github.com/pre-commit/mirrors-mypy' appears to be a mutable reference (moving tag / branch). Mutable references are never updated after first install and are not supported. See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details. Hint: `pre-commit autoupdate` often fixes this.
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...............................................................Passed
black....................................................................Passed
isort....................................................................Passed
dotenv-linter........................................(no files to check)Skipped
yamllint.................................................................Passed
flake8...................................................................Passed
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1
Error constructing plugin instance of NewSemanalDjangoPlugin
Traceback (most recent call last):
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/mypy/__main__.py", line 12, in console_entry
main(None, sys.stdout, sys.stderr)
File "mypy/main.py", line 96, in main
File "mypy/main.py", line 173, in run_build
File "mypy/build.py", line 180, in build
File "mypy/build.py", line 231, in _build
File "mypy/build.py", line 478, in load_plugins
File "mypy/build.py", line 456, in load_plugins_from_config
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/mypy_django_plugin/main.py", line 143, in __init__
self.django_context = DjangoContext(django_settings_module)
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/mypy_django_plugin/django/context.py", line 95, in __init__
apps, settings = initialize_django(self.django_settings_module)
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/mypy_django_plugin/django/context.py", line 77, in initialize_django
settings._setup()
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 71, in _setup
self._wrapped = Settings(settings_module)
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 179, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/**/project/config/settings/local.py", line 3, in <module>
from .base import * # noqa
File "/home/**/project/config/settings/base.py", line 6, in <module>
import environ
ModuleNotFoundError: No module named 'environ'
I have to write some more text here, because stackoverflow get error: a lot of code, add details.
I'm new to Django. Trying to migrate from django==1.8.5 to 1.11
django-hvad package is essential for me, however, when upgraded to django==1.10, it throws the assertion error about the Django version.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f407d733b70>
Traceback (most recent call last):
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/apps/config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "/home/riddle/pro/blog/env/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/riddle/pro/blog/app/apps/frontend/website/models.py", line 116, in <module>
class ContentBlock(Common, TranslatableModel, OrderedModel):
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/db/models/base.py", line 309, in __new__
new_class._prepare()
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/db/models/base.py", line 359, in _prepare
signals.class_prepared.send(sender=cls)
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 191, in send
response = receiver(signal=self, sender=sender, **named)
File "/home/riddle/pro/blog/env/lib/python3.6/site-packages/hvad/models.py", line 447, in prepare_translatable_model
assert django.VERSION < (1, 10)
AssertionError
My project is actually a single-page website, so it is not a big hassle to remove django-hvad and use something else. That is my question therefore, what steps are better to take:
(1) try to use another "app" for model translation, as in:
https://djangopackages.org/grids/g/model-translation/
(however, as I see, only one package supports Django1.10 there).
(2) Or maybe Django has a native model translation option which I should use.
(3) another option
The strange thing also is that https://django-hvad.readthedocs.io/en/latest/index.html indicates that django1.9 is the last supported version, but in GitHub page https://github.com/kristianoellegaard/django-hvad it is written that django 1.10 as well as django 1.11 are supported.
Please indicate what more information is needed, I will try to provide.
My pip freeze output:
Django==1.10
django-appconf==1.0.2
django-compressor==2.2
django-filebrowser-no-grappelli==3.7.4
django-geoposition==0.3.0
django-hvad==1.8.0
django-jquery==3.1.0
django-libsass==0.7
django-ordered-model==1.4.3
django-tinymce==2.7.0
libsass==0.14.2
Pillow==5.1.0
psycopg2-binary==2.7.4
pytz==2018.3
rcssmin==1.0.6
rjsmin==1.0.12
six==1.11.0
Usage of django-hvad (example):
from hvad.models import TranslatableModel, TranslatedFields
class SliderBox(TranslatableModel, OrderedModel):
translations = TranslatedFields(
image = FileBrowseField(
"Image",
max_length=200,
directory="uploads/",
extensions=[".png", ".jpg"],
blank=True,
null=True
),
text = models.CharField(max_length=200),
)