My settings are structured like:
/settings/__init__.py
/settings/base.py
/settings/dev.py
/settings/prod.py
The constant RANDOM_VAR is set in dev.py
When I do the following in e.g. urls.py
from django.conf import settings
print(settings.RANDOM_VAR)
I get
AttributeError: 'Settings' object has no attribute 'RANDOM_VAR'
After further testing I see that all my database settings etc. are loaded from dev.py. But when I want to access my dev.py settings through from django.conf import settings it doesn't work.
I don't want to use from <your_path>.settings import dev, because this would not work on production.
Any ideas?
Your settings structure should be like this,
/settings/__init__.py
/settings/base.py
/settings/dev.py
/settings/prod.py
the you can try something like this
from <your_path>.settings import dev
print(dev.RANDOM_VAR)
Hope this helps you
Related
Quick (probably silly) question regarding Django-Filebrowser. I'm adding the lines from readthedocs in my settings.py
MEDIA_ROOT = getattr(settings, "FILEBROWSER_MEDIA_ROOT", settings.MEDIA_ROOT)
and I'm getting
NameError: name 'settings' is not defined
If I'm right, adding an import
import filebrowser.settings
Will throw a circular reference error (it comes up asking for a SECRET_KEY) if I do so. What do I do to fix this and define settings?!
Thanks!
By looking at the source it looks like you should import Django settings:
from django.conf import settings
My code is here.
I have tried different approach from stackoverflow and non of them worked.
import os
import sys
from django.conf import settings
sys.path.append('/var/www/iaas/horizon')
sys.path.append('/var/www/iaas/horizon/openstack_dashboard')
os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'
from bill.models import MonthlyBills
from django.contrib.auth import models
If I run python daemonize.py, here is the error message I get.
I am confused because I have already included my django project path in my sys.path
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'openstack_dashboard.settings' (Is it on sys.path?): cannot import name connection
What I am trying to achieve is to create a python-daemon, I need to have an access in my django models.
I hope someone who could point me where I am mistaking here.
You have to set up os.environ['DJANGO_SETTINGS_MODULE'] before you import settings.
The process of importing django.conf.settings will look to see if the DJANGO_SETTINGS_MODULE environment variable is set before determining white settings to load.
import os
import sys
sys.path.append('/var/www/iaas/horizon')
sys.path.append('/var/www/iaas/horizon/openstack_dashboard')
os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'
from django.conf import settings
i am trying to import the client in django for testing. but when i do, i get this wierd error:
ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
from django.utils import unittest
from django.utils import simplejson as json
from django.test.client import Client
this is how i imported the client so that i could use it for testing. can someone explain this to me please.
Try this:
import os
import sys
sys.path.append('/home/username/www/site_folder')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from django.utils import unittest
from django.utils import simplejson as json
from django.test.client import Client
But replace project with folder name, where your settings.py is
The Client is looking for the settings.py. You could simply load the client by typing this in your project folder:
python manage.py shell
In Pycharm which I use, after following this Running Django tests in PyCharm
my problem was solved.
It's in the file > settings > Django Support, and then select the right settings.
I'd like to iterate through the INSTALLED_APPS in my Django settings file like so:
#task
def test_app
print settings.INSTALLED_APPS
How do I access my settings file in a fabfile?
Thanks.
It's documented: http://docs.fabfile.org/en/latest/api/contrib/django.html
from fabric.contrib import django
django.settings_module('myproject.settings')
from django.conf import settings
I had a small proof of concept set up on a development server on a local machine. I'm now trying to move it over to django on a production server, which I'm using webfaction for. However, now that I'm switched over to apache from the built in django server I get the following:
ViewDoesNotExist: Could not import orgDisplay.views. Error was: No module named orgDisplay.views
But when check my orgDisplay apps folder there is a view.py in it. What am I doing wrong? I've tried adding the following to my settings.py by suggestion of the django IRC room.
import sys
sys.path.append(r"/home/user/webapps/django_project/myproject/orgDisplay")
which is the path to my app.
any ideas on how to even begin to trouble shoot this?
Thanks in advance.
I assume you're using mod_wsgi (which is recommended by Django authors), not mod_python. This is the way you should use your sys.path:
django.wsgi:
import os, sys
sys.path.append(r"/home/user/webapps/django_project/myproject/")
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
sys.stdout = sys.stderr # Prevent crashes upon print
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
urls.py:
from django.conf.urls.defaults import *
urlpatterns = (
("", include("orgDisplay.urls")),
# ...
)
orgDisplay/urls.py:
import views
urlpatterns = (
(r'^some_view/$', views.some_view), # It is actually orgDisplay.views.some_view
# many more records ...
)
It is a bad idea to add project dir itself to path since you're be getting name conflicts between multiple projects.
I think you're appending the wrong directory to sys.path. I think Python is looking in the .../myproject/orgDisplay folder for the orgDisplay package. Try removing the orgDisplay from your string, like this:
import sys
sys.path.append(r"/home/user/webapps/django_project/myproject")
The other option would be to simply add myproject (or whatever your project is actually called) in the import statement.
# instead of "from orgDisplay import views"
from myproject.orgDisplay import views
Also, make sure to restart Apache after every edit.
looking at manage.py, it does it like so:
import sys
from os.path import abspath, dirname, join
from django.core.management import setup_environ
# setup the environment before we start accessing things in the settings.
setup_environ(settings_mod)
sys.path.insert(0, join(PINAX_ROOT, "apps"))
sys.path.insert(0, join(PROJECT_ROOT, "apps"))
Provided your WSGI file is in your project directory, a slightly more flexible way is this:
import os, sys
sys.path.append(os.path.dirname(__file__))
This will enable you to change your project location later without having to modify your WSGI file.