Import Error While Executing Django Tutorial - python

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.

Related

Django, importing models class into new file. Import error attempted relative import with no known parent

I am working on learning Django by making a simple analysis dashboard. I did the startproject command and the startapp command like the tutorial ran through. I added a new file called connectionOracle.py in the app folder.
My folder structure is (top folder is was created via venv)
AnalysisSite
|AnalysisSite
|coreAnalysis
||models.py
||connectionOracle.py
I have a class called WorkOrder in the models.py file. I am trying to import it into the connectionOracle.py file by doing the following
from .models import WorkOrder
I then go to the Command Line (on windows) and navigate tot he coreAnalysis folder and run the following
python connectionOracle.py
I get an error that says.
ImportError: attempted relative import with no known parent package
I did some reading online, and I tried doing an absolute path with AnalysisSite.AnalysisSite.coreAnalysis.models
that didnt work. I also tried moving the connection file to different directories and that didnt work either. I also tried going into the command line and typing set DJANGO_SETTINGS_MODULE = AnalysisSite.settings
I also put a _init_.py in each folder. (Django automatically put it into the project directory and app directory).
I am not sure what I am doing wrong
you are trying to access Django components(models) by a script file ,
the caller in this case not Django itself ( the request not coming from url or different django tools or mechanism),
anyway in your custom python file which is 'connectionOracle.py'
try to do some steps before accessing the models itself,
the steps are available on the following URL:
https://stackoverflow.com/a/68936419/12662056
############
change the path for project depending on your project path.
i hope this helpful

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

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.

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.

Models missing error while migrating Django project from dev to production

I have setup a Django project on my laptop and build my first app. I have setup all the necessary software on a second server and I want to migrate my project to that one.
After uploading my files, I have tried a couple of things, but I still get errors like:
ImportError at /
No module named myapp.models
Request Method: GET
Django Version: 1.3.1
Exception Type: ImportError
Exception Value:
No module named myapp.models
Exception Location: /var/www/wsgi/myproject/myapp/admin.py in <module>, line 2
Python Executable: /usr/bin/python
Python Version: 2.6.5
The problematic line on admin.py looks like:
from myapp.models import (...)
and file models.py in this app has the necessary stuff, so I guess it can't resolve the app namespace or something?
It seems your PYTHONPATH doesn't include /var/www/wsgi/myproject. Can you show your WSGI config file?
You may need to move/create an __init__.py file in your app directory.

Trouble running example django code

Im trying to learn Django by looking at examples, but Im having a bit of a problem running the examples I find.
I downloaded 'cheeserator' from https://github.com/jacobian/cheeserater
and I tried running it with python manage.py runserver
but I get the following error -
Error: Can't find the file
'settings.py' in the directory
containing 'manage.py'. It appears
you've customized things. You'll have
to run django-admin.py, passing it
your settings module. (If the file
settings.py does indeed exist, it's
causing an ImportError somehow.)
What am I doing wrong?
You need to have a settings.py file.
As per the instructions in the link provided:
Then you'll want to create a settings.py file in this directory containing::
from settings_template import *
# Override any settings you like here.
Or if you don't want to override anything rename settings_template.py to settings.py

Categories