I'm documenting a django project of mine but got stuck with some errors when I run make html.
If I go to the .py file where the error is happening and comment the imports at the beginning of the file, the make html command runs flawlessly.
And this is part of the error message:
ImproperlyConfigured: Requested setting CACHES, but settings are not
configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing
settings.
I tried this Sphinx Docs not importing Django project settings but nothing.
I found on this on this website(link), and worked for me.
I had to include it in my conf.py file.
sys.path.insert(0, os.path.abspath('..'))
from django.conf import settings
settings.configure()
Related
I am newbee in Django.I finished the Django Tutorial once from the official djangoproject site.Not saying I know everything now, but everything worked perfectly for me till I was referring that website.
I started referring to the Practical Django projects book and somewhere in my models I tried to import :
from django.contrib.auth.models import User
gives me an import error :
ImportError Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined
`
What is really going on here ? I have been successfully importing all the packages till now then why this sudden error ?
How are you running the code that gives you the error? If you are importing the models.py in the shell, use the Django shell command
./manage.py shell
If you are trying the import in another script, you have to set the environment variable DJANGO_SETTINGS_MODULE manually.
import os
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
This requires myproject to be in your python path.
See this similar Stack Overflow question for futher discussion.
Following on from my last question Error: No module named psycopg2.extensions, I have updated my mac OS to Mountain Lion and installed Xcode. I have also installed psycopg2 using 'sudo port install py27-psycopg2'. I am now trying to run 'python manage.py runserver' but am receiving this error
AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'
Any help on how to fix this and get my localhost running?
From django docs:
A Django settings file contains all the configuration of your Django installation.
When you use Django, you have to tell it which settings you're using.
Do this by using an environment variable, DJANGO_SETTINGS_MODULE.
The value of DJANGO_SETTINGS_MODULE should be in Python path syntax,
e.g. mysite.settings. Note that the settings module should be on the
Python import search path.
And
ROOT_URLCONF
Default: Not defined
A string representing the full Python import path to your root
URLconf. For example: "mydjangoapps.urls". Can be overridden on a
per-request basis by setting the attribute urlconf on the incoming
HttpRequest object. See How Django processes a request for details.
If you are using multiple settings files, be sure to import the base settings in all the other settings files. This was how I got this error.
If you're working on a Django project, the root of your project(ROOT_URLCONF= variable) isn't defined in your settings.py file, or at least it isn't defined properly.
If you don't have a settings.py file, this block of code should fix the issue:
from django.conf import settings
settings.configure(
# ...
ROOT_URLCONF=__name__,
# ...
),
)
What that does is specify the root of your project runserver knows where to find your project.
If do have a settings.py file, then find that variable and add __name__ to it.
I've a setting say gi in settings.py file. I've created a separate python file (i.e. I'm not using it in views) and used import statement as:
from django.conf import settings
but when I try to access settings.gi, it says that 'Settings' object has no attribute 'gi'. What's missing? :s
From the Django docs on creating your own settings states:
Setting names are in all uppercase.
Try renaming the setting to GI.
The project must be added on the PYTHONPATH and the DJANGO_SETTINGS_MODULE has to be defined.
Please refer to the commands mentioned below.
export PYTHONPATH=PATH_OF_PROJECT
export DJANGO_SETTINGS_MODULE=settings
I am running Django in a virtual environment (using virtualenv), and I'm trying to add a custom development environment settings file to simplify app configuration when I'm developing. My plan was to do this with two lines of code
if os.environ.get('DEVELOPMENT', None):
from login import settings_dev
I've also tried import settings_def and from login.settings_dev import *. My settings_dev.py file is sitting in the same directory as my settings.py file and my app is sitting in a folder called login. When I run python login/manage.py syncdb I get this error:
Error: Import by filename is not supported.
My searching keeps bringing up DJANGO_SETTINGS_MODULE (though I'm not sure how it plays into all this - first Django app :]), so just an FYI it is set in my settings.py file like so:
os.environ['DJANGO_SETTINGS_MODULE'] = 'login.settings'
I've also tried exporting it in my terminal, but I get the same error.
Does anyone know how I can fix this/what I'm doing wrong here?
Make sure while passing relative address of file to use "." instead of "/".
I faced the same error what I actually did
"music/urls"
But it should be
"music.urls"
In the original settings.py, at the very end:
try:
from settings_dev import *
except ImportError:
pass
Create settings_dev.py in the same directory as settings.py, and in it, add these two lines at the very top:
import sys
globals().update(vars(sys.modules['settings']))
Now add whatever development settings you want in this file.
I had similar error in runserver command execution and finally I've found that this error raises because of python version incompatibility by the django version installed. There is two versions of python on my system and I had running django server by the wrong one. Hope it could be helpful to someone.
When I import it inside a handler with this:
from django.core.validators import email_re
I get the following error when I run the app:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django/django/conf/__init__.py", line 53, in _import_settings
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.
It's unclear to me how to set that environment variable in a webapp project. Do I need to add some additional settings.py file? Any examples?
Try using following lines of code after your imports:
from django.conf import settings
settings.configure()
Read here for more info: http://groups.google.com/group/tipfy/browse_thread/thread/78c17b4f82c894a6