AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF' - python

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.

Related

python: Django import fails asking me to define DJANGO_SETTINGS_MODULE

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.

Import Error While Executing Django Tutorial

I am following Django tutorial given in the following link:-
http://docs.djangoproject.com/en/1.4/intro/tutorial01/
While running server I get many error messages:-
ImportError at /admin/
No module named polls.urls
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.4.1
Exception Type: ImportError
Exception Value:
No module named polls.urls
Exception Location: C:\Python27\lib\site-packages\django\utils\importlib.py in import_module, line 35
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3
Python Path:
['D:\\chetan\\All_My_Projects\\mysite1',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
The errors are similar to what has been asked in:-
No module named urls
My question is, do we need to add the project directory,which in my case would be D:\chetan\All_My_Projects\mysite1 to environment variable? Is this causing the problem..
This is the whole Django Project ( I suppose, you are on Windows, using Py 27 )
Follow these steps:
open cmd, direct it to d:\, Now python c:/python27/lib/site-packages/django/bin/django-admin.py startproject mysite
cd mysite and python manage.py startapp polls
Now, cd polls
# polls/views.py
from django.shortcuts import render_to_response
def polls_home(request):
return render_to_response("polls.html")
# polls/urls.py
from django.conf.urls import patterns, include, url
import views
urlpatterns = patterns ("",
url(r'^polls$', views.polls_home),
)
Go to d:/mysite/mysite/settings.py and add polls in INSTALLED_APPS
very important: open mysite/mysite/urls.py
add url(r'^home$', include('polls.urls') ), to url patterns
Now, runserver and open /home/polls
Is it possible that you have typed in <your_app>/urls.py:
url(r'^polls/', include(polls.urls))
Instead of this? (the quotes are needed):
url(r'^polls/', include('polls.urls'))
I would check if polls is in INSTALLED_APPS inside <your_app>/settings.py if that doesn't work.
The Django tutorial executed properly after adding my project in PythonPath.i.e, my project was created in 'D:\chetan\All_My_Projects\mysite1'. All i had to do in Aptana Studio is:-
1.Go to Windows->Preferences->PyDev->Interpreter-Python
2.Add 'D:\chetan\All_My_Projects\mysite1' to the PythonPath.
Thank you all for your help. I would also like to refer to the following link which has emphasized on adding the project to the PythonPath:-
http://www.webmonkey.com/2010/02/Install_Django_and_Build_Your_First_App
The tutorial could benefit from using absolute paths, e.g. mysite/mysite/urls.py, etc. to avoid confusion. This is why...
The django tutorial is a bit confusing because it creates a project mysite (which most of us are going to give a different name) and then within that project mysite it also has a site called mysite.
One mysite directory is used for starting the server because it contains manage.py
The other nested mysite contains the urls.py which maps to the new app (the tutorial calls this app polls).
So although the tutorial clearly explains what the directory structure should look like, if one tries to follow it too quickly it's not immediately obvious that the app polls is supposed to reside side-by-side the site mysite). thus creating mysite/mysite, mysite/polls or perhaps djangotest/djangotest, djangotest/myapp, etc.
When following this tutorial, if one had created the necessary files inside the wrong directory, it won't be immediately obvious what that person had done wrong resulting in the error No module named polls.urls.

Unable to get a setting from settings file in django

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

Using template in Django tutorial

I'm on Chapter 4: Templates of the Django tutorial, and I'm trying to run the following in an Emacs inferior mode using Python:
>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> c = template.Context({'name': 'Adrian'})
>>> print t.render(c)
My name is Adrian.
>>> c = template.Context({'name': 'Fred'})
>>> print t.render(c)
My name is Fred.
But I get the error here.
A quick search suggests that I should do be setting my DJANGO_SETTINGS_MODULE like so:
>>> import os
>>> os.environ["DJANGO_SETTINGS_MODULE"] = "settings.py"
However, I run into this error:
ImportError: Could not import settings 'settings.py' (Is it on sys.path?): No module named settings.py
I'm using Fedora 16. How do I correctly resolve this problem?
You can do this in the python manage.py shell, since that would have settings.py in . directory.
Otherwise:
Consider settings.py just another python library and you need to ensure its added to sys.path and then you can do as follows :
import settings
from django.core.management import setup_environ
setup_environ(settings) # Setting up the env settings
This sets up the settings to your environment. [ This is the right way ]
There is no secret recipe in settings.py and since django in itself is a library to python, so it could not be default set any paths to your sys.path unless imported and executed - as I suggested.
You should really use setup_environ as suggested in another answer.
A hint regarding this specific exception: DJANGO_SETTINGS_MODULE is a Python module name, so it may not end with ".py". Usually, this would be your_django_site.settings and the module/directory your_django_site has to be in sys.path.
All Django code not in a project should either be run from the Django REPL, available via ./manage.py shell, or run as a standalone script.
In order to test anything in the shell for Django, you should use the Django shell:
python manage.py shell
This will start up a Python interpreter which will magically have your settings file (and the rest of your project) on the Python path.
(This assumes you have a project already. If not, use django-admin.py startproject my_project_name.)

Django settings.py Error: Import by filename is not supported

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.

Categories