Using Constants in Settings.py - django

Can I use a variable declared in the Django project's settings.py in one of my module files?
For instance, using DATABASE_HOST = 'databasename'?
I'm trying to get the name of the server the application is currently deployed on you see.

You certainly can... it's encouraged, in fact. To use it, import the settings from django.conf (this imports your project's settings):
from django.conf import settings
print "My database host is %s" % settings.DATABASE_HOST
The documentation on Using settings in Python code explains why this works, and why this is preferable over importing the the settings.py module directly.

yes
from django.conf import settings
print settings.MY_SETTINGS_VAR

Related

How to create a django package without setting DJANGO_SETTINGS_MODULE as environment variable?

I am creating a package that itself uses Django and I will be using it within other Django applications. The main issue I am facing is that I need to use to settings for various reasons such as logging and other extensive requirements. Since, this package does not have any views/urls, we are writing tests and using pytest to run them. The tests will not run without the settings configured. So initially I put the following snippet in the __init__ file in the root app.
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_package.settings")
django.setup()
Now, the test ran properly and the package as standalone app was working. But the moment I installed it in the main project, it overrides the enviroment variable with it's own settings and you can imagine the kind of havoc it would ensue.
This is the first time I am packaging a django app. So I am not well-versed with best practices and the docs are a little convoluted. I read the structure and code of various packages that use settings in their package but I am still not able to understand how to ensure the package accesses the intended settings and the project's settings is not affected at the same time.
While going throught the docs, I came accross this alternative to setting DJANGO_SETTINGS_MODULE, like this:
from django.conf import settings
settings.configure(DEBUG=True)
As shown here: https://docs.djangoproject.com/en/2.2/topics/settings/#using-settings-without-setting-django-settings-module
But where exactly am I supposed to add this? To every file where the settings are imported or will it work in the __init__ (Tried this but something isn't right, shows Apps aren't loaded )
I tried this as well where I imported my settings as defaults and called configure using them as defaults and called django.setup() as well but didn't do the trick:
# my_package/__init__.py
from django.conf import settings
from my_package import settings as default
if not settings.configured:
settings.configure(default_settings=default, DEBUG=True)
import django
django.setup()
Also, I need settings mainly because I have few parameters that can be overridden in the project that is using the package. When the package is installed, the overridden variables is what I should be able to access in the package during runtime.
If someone can guide on how to tackle this or have a better process of creating packages that need django settings, please do share.
So I ended up finding a way to work without setting the settings module as an environement variable. This enables me to use the specified settings by importing all the overridden settings as well as the default settings from:
Create a apps file for configuring your package as an app.
# my_package/apps.py
from django.apps import AppConfig
class MyPackageConfig(AppConfig):
name = 'my_package'
verbose_name = 'My package'
And, in your package's root. The following snippet in your __init__.py will only set the overridden settings:
# my_package/__init__.py
from django.conf import settings
import django
from my_package import settings as overridden_settings
from django.conf import settings
default_app_config = 'my_package.apps.MyPackageConfig'
if not settings.configured:
# Get the list of attributes the module has
attributes = dir(overridden_settings)
conf = {}
for attribute in attributes:
# If the attribute is upper-cased i.e. a settings variable, then copy it into conf
if attribute.isupper():
conf[attribute] = getattr(overridden_settings, attribute)
# Configure settings using the settings
settings.configure(**conf)
# This is needed since it is a standalone django package
django.setup()
Reference for what django.setup() will do:
https://docs.djangoproject.com/en/2.2/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
Points to keep in mind:
Since it is in the __init__, this will make sure if you import something from the package, the settings are configured.
As mentioned in the documentation above, you have to make sure that the settings is configured only once and similarly the setup method is called once or it will raise an Exception.
Let me know if this helps or you are able to come up with a better solution to this.

Applying Django 1.6 project settings in PyDev

I have no problems running the python shell with the python manage.py -shell command in the terminal; I can import my modules and make queries on the database and so on. However, in PyDev, even though I can import modules, when I try to access the data stored in my SQLite database, I get this message:
ImproperlyConfigured: settings.DATABASES is improperly configured.
Please supply the ENGINE value.
Since my project's settings are ok (the site works fine locally), it must have to do with Pydev not applying the project configs. The sequence of starting up the Django/python shell is as follows:
from django.conf import settings; settings.configure()
from django.core import management
import XX.settings as settings
management.setup_environ(settings) # This throws an error as setup_environ
# setup_environ is deprecated in Django 1.6
The last 3 lines are hard-coded (and were, I gather, working pre-Django 1.6)
I thought doing something like:
from django.conf import settings as djangoSettings
from XX import settings
djangoSettings.configure(settings)
But then I get this error:
ImportError: Could not import settings ''XX.settings'' (Is it on
sys.path? Is there an import error in the settings file?): No module
named 'XX.settings'
And yes, the path is in sys.path.
Any help greatly appreciated.

Best practices to integrate a django app with other django apps

In Django:
a) What is the best way to test that another app is installed? (By installed I mean to be in INSTALLED_APPS)
b) What is the recommended way to alter the behaviour of the current app accordingly. I understand that:
if "app_to_test" in settings.INSTALLED_APPS:
# Do something given app_to_test is installed
else:
# Do something given app_to_test is NOT installed
is possible, but is there another way? is this the recommended way?
c) What is the recommended practice to import modules that are only required if another app is installed? import then inside of the if block that test for the installed app?
I tend to favour checking INSTALLED_APPS as you have listed in your question.
if DEBUG and 'debug_toolbar' not in INSTALLED_APPS:
INSTALLED_APPS.append('debug_toolbar')
INTERNAL_IPS = ('127.0.0.1',)
This works well when you have settings distributed across different settings files that don't necessarily have knowledge of the other. eg I might have a shared_settings.py which contains a base set of INSTALLED_APPS, then a debug_settings.py which imports shared_settings.py and then adds any additional apps as required.
The same applies for non-settings. For example, if you have Django South installed and want to create introspection rules for South only if it's installed, I would do this:
if 'south' in settings.INSTALLED_APPS:
from south.modelsinspector import add_introspection_rules
# Let South introspect custom fields for migration rules.
add_introspection_rules([], [r"^myapp\.models\.fields\.SomeCustomField"])
As I see it, there's no need to try and import a module if you know there's the possibility that it may not be installed. If the user has listed the module in INSTALLED_APPS then it is expected to be importable.
That?
try:
# Test it
from an_app import something
except ImportError as e:
from another_app import something
#Do something else

Sphinx Docs not importing Django project settings

I just recently move a Django project into a new virtualenv. The project works fine, but I am having trouble building my Sphinx Documentation.
in my conf.py I have this:
import sys, os
sys.path.append('/path/to/myproject')
from django.core.management import setup_environ
from myproject import settings
setup_environ(settings)
But when I use make html I get this error:
from myproject import settings
ImportError: No module named myproject
Any help much appreciated.
Turns out the conf.py needs to look like this:
import sys, os
sys.path.append('/path/to')
from myproject import settings
from django.core.management import setup_environ
setup_environ(settings)
Hope this might help someone.
Django 1.4 deprecated setup_environ. Here's similar code for Django 1.4 and later:
import sys, os
cwd = os.getcwd()
parent = os.path.dirname(cwd)
sys.path.append(parent)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
Having this problem with a Django 1.7.5 project and I think it's down to some strange project layout decisions we made, but I needed one extra step to solve this using jwhitlock's answer:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.conf import settings
When I did that useless import it found my custom Django settings which were specified in DJANGO_SETTINGS_MODULE but were not found by the autodoc process. I think this is because the project lives in a folder whose parent has the same name, but inspecting sys.path only shows the "right" folder so the import should work but blows up saying it can't find my settings.

Cannot import django.core

I am trying to setup django with fastcgi on apache. Django is installed and seems to be running correctly but I am having problems with setting up fastcgi.
I decided to test out my dispatch.fcgi script in the interactive python shell line by line and the following line:
from django.core.servers.fastcgi import runfastcgi
results in the following error:
ImportError: No module named core.servers.fastcgi
I can import django with no problem but import django.core gives yet another ImportError (No module named core).
How can I go about ensuring that I can import django.core. If I can import django then in must be on my path, and so why can I not import core?
You probably have a file/folder called django somewhere in your path, that isn't the actual path.
try this
import sys
sys.path
And then check everything in that output to see if there is a file/folder called django(.py) somewhere.
If so, change the path (sys.path = ['/path/to/directory/below/django/install'] + sys.path) or move/rename the file.
Probably you have django.py module in your working directory or in any other that is in python path.
For anyone having this problem and coming across this question like I did, it turns out that fastcgi support has been removed as of Django 1.9, thus you will get this import error if trying to import it. Refer Using Django with virtualenv, get error ImportError: No module named 'django.core.servers.fastcgi'