Wagtail settings only use .dev - django

I do not understand how to alternate between production and dev settings. Wagtail docs do not cover it and the only wagtail tutorial I can find mentions it and then completely skips over it. There is a settings file:
--| settings
----| __init__.py
----| base.py
----| dev.py
----| production.py
----| .env
my init file:
import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
ENV = os.environ.get('AMSS_ENV')
if ENV == 'dev':
from .dev import *
elif ENV == 'prod':
from .production import *
AMSS_ENV is set to 'prod'. I also have the DJANGO_SETTINGS_MODULE variable set to production in the .env from a different attempt. Does the init file not fire first? is my logic broken? I get no errors and everything works but it loads in dev every time. I've tried so many other things and it just sticks like this. Can someone tell me what am I supposed to do? or where I can look?

It is always useful to check wsgi.py and manage.py to see which settings file they are set to. It is very easy to accidentally serve or run commands with the wrong settings file by forgetting about these two files.

Related

How do I import settings when using DJANGO_SETTINGS_MODULE?

I have a Django project and am using the DJANGO_SETTINGS_MODULE for managing each environment settings. I have created different settings files for each environment, however, I'm confused how I can access these settings without having to change the imports in my code. For example, I have this settings directory structure:
stats/
config/
settings/
__init__
base.py
dev.py
prod.py
base.py has all my default settings, include some API keys for third party services. I override these in each environment:
base.py
API_KEY = "default"
dev.py
from base import *
API_KEY = "dev"
prod.py
from base import *
API_KEY = "prod"
When I start up my server, I use
./manage.py runserver --settings=stats.config.settings.dev
But how do I access the API_KEY without having to change "dev" to "prod" every time I deploy? This is how I currently access the API_KEY in my code:
from stats.config.settings.dev import API_KEY
I can't seem to find the proper answer to this anywhere. Thanks!
You access your settings via django.conf.settings, you're not supposed to import/access them directly
from django.conf import settings
settings. API_KEY

Issue loading admin.site.urls after changing the settings folder

I re-organized Django,the following way:
config
- settings
- base.py
- local.py
urls.py
wsgi.py
In base.py/local.py:
ROOT_URLCONF = 'config.urls'
WSGI_APPLICATION = 'config.wsgi.application'
In manage.py I changed:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
In wsgi.py I changed:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
I have the following error on runserver:
\django\contrib\admin\sites.py", line 269, in get_urls
path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
AttributeError: 'AccountAdmin' object has no attribute 'urls'
It is related to this line:
path('admin/', admin.site.urls), # Django 2.0 syntax
If I comment that line I get the following error:
django\contrib\admin\sites.py", line 79, in check
if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'AccountAdmin' object has no attribute 'model
The app admin is in installed app, I don't know what is creating this issue.
Hmm... Several things happen here. One thing at a time:
Under your settings dir put an __init__.py file with the following contents in it:
from .base import *
try:
from .local import *
LIVE = False
except ImportError:
LIVE = True
if LIVE:
try:
from .production import *
except ImportError:
pass
By putting this inside the __init__.py file, you can reference to your settings file simply with 'config.settings', leaving local or production unreferenced (the __init__.py will handle them).
Now that this is out of way, change both uwsgi.py and manage.py to:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
Assuming that you have done all that, it should work (that's how I structure my projects for years and had never any problems). Otherwise, please update your question with project structure and base.py and local.py contents to work it out.

Django: how to set the path for the environ variable "DJANGO_SETTINGS_MODULE"

In Django, I used to write populating scripts and put them in the project root directory. For example,
mysite/
mysite/
manage.py
populateA.py
The first few lines of populateA.py:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
import django
django.setup()
...
As there are more and more populating scripts, I would like to move them to another package populate:
mysite/
mysite/
manage.py
populate/
__init__.py
populateA.py
populateB.py
populateC.py
...
However, when I run the populating scripts (python populateA.py), I got the error message: ImportError: No module named 'mysite'. How to properly set the path for DJANGO_SETTINGS_MODULE?
Since populate is already a module, run its submodules.
python -m populate.populateA

Sphinx autodoc not importing modules

I am writing some documentation for a Django project using Sphinx.
My project (Django 1.4) looks like this:
/funproject
/documentation
# this is where sphinx files live
/source
conf.py
...
/funproject
__init__.py
settings.py
admin.py
models.py
/app1
/app2
fabfile.py
manage.py
I have Sphinx setup in the conf.py:
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'funproject.settings'
from funproject import settings
from django.core.management import setup_environ
setup_environ(settings)
So in my Sphinx source files I can do this:
.. automodule:: app1.models
:members:
which works fine. However, these two examples do not work:
1.
.. automodule:: funproject.models
:members:
.. automodule:: fabfile
:members:
The second works if I move the fabfile into app1 and use app1.fabfile
I'm guessing I have some issue with my conf.py but I have tried many derivations but I cannot import my fabfile.py unless it is under an app dir (though under /funproject it doesn't work either).
You might try using the fabric fabfile directory style? Perhaps it's the case that automodule doesn't look at single file modules? Docs on this other style are here

Django ImportError for models.py

(Updating my question with more information.)
My django app is running fine on my dev server.
I have a view that pulls from the database using the below line that works fine:
from myapp.models import MyTable
However, if I add the above 'from/import' to another module (see below structure, it's the module named 'problem_module.py') I'm writing where I want to pull from the sqlite3 database, I get this error.
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myfolder.settings' (Is it on sys.path?): No module named myfolder.settings
I've read and tried various solutions recommended when people get this error, but I missing something because i'm unable to resolve it.
I'm using Django 1.4 and have the lay-out as recommended.
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
myapp/
__init__.py
models.py
admin.py
views.py
indevelopment/
__init__.py
problem_module.py
I figured out what was happening and why after going through the traceback carefully and looking at the django source code. Here is what happens.
When you run:
python manage.py runserver
the environment variable gets set properly assuming you already changed this small little file or just don't pay attention to it because django 1.4 automatically configures it for you.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
However, because this setting of os.environ is under a:
if __name__ = "__main__"
expression, it only gets run if call that file directly, as you do with:
python manage.py runserver
Otherwise, if you are running a file that needs that environment variable - say testing a module in Eclipse - , the os.environ needs to get set in another place (shell, etc).
All the that I got generally pointed to this but I needed the context.
But as a little adjustment (yes, not a good idea as it couples) on the source code you can also hardcode it in manually in/django/conf/__init__.py
Specifically to see where it happens, the change below works:
# in module: /django/conf/__init__.py
class LazySettings(LazyObject):
def _setup(self):
try:
# Comment out the call to os.environ and hardcode in your app settings
# settings_module = os.environ[ENVIRONMENT_VARIABLE]
# WARNING: bad practice to do this. ;.
settings_module = "myapp.settings"
Have you changed/set DJANGO_SETTINGS_MODULE?
Try export DJANGO_SETTINGS_MODULE=mysite.settings and start your dev server.
modify your manage.py:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
PyCharm sometimes override DJANGO_SETTINGS_MODULE to empty string. Try to debug your manage.py and see if it realy changes after setdefault() call.
If its not either change pycharm settings or use os.environ['DJANGO....']='my_settings'..
or hack files at .idea/. .idea/workspaed.xml contains
env name="DJANGO_SETTINGS_MODULE" value="" in this case