views:

49

answers:

2

I'm using Ubuntu 10, python 2.6.5

I'm following this tutorial: http://www.djangobook.com/en/2.0/chapter02
I followed all of the steps using cut-and-paste. The following directory structure was automatically created:

bill@ed-desktop:~/projects$ ls -l mysite  
total 36  
-rw-r--r-- 1 bill bill     0 2010-09-01 08:18 __init__.py  
-rw-r--r-- 1 bill bill   546 2010-09-01 08:18 manage.py  
-rw-r--r-- 1 bill bill 20451 2010-09-01 18:50 mysite.wpr  
-rw-r--r-- 1 bill bill  3291 2010-09-01 08:18 settings.py  
-rw-r--r-- 1 bill bill   127 2010-09-01 11:13 urls.py  
-rw-r--r-- 1 bill bill    97 2010-09-01 08:20 views.py  

urls.py

from django.conf.urls.defaults import *
import sys
print sys.path

from mysite.views import hello
urlpatterns = patterns('',
    (r'^hello/$', hello),
)

pylint produces this error: Unable to import 'mysite.views'

views.py

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello world")


bill@ed-desktop:~/projects/mysite$ python manage.py runserver 

Validating models...
0 errors found

Django version 1.2.1, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Which resulted in:

Page not found (404)
Request Method:  GET
Request URL:  http://127.0.0.1:8000/

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
   1. ^hello/$ 
The current URL, , didn't match any of these. 

Why does view.py which is in the main directory contain the following?

from mysite.views import hello

There is no subdirectory 'views'. Although I'm familiar with using packages, I've never had the need to create my own so I'm a bit confused. I would have thought that from views import hello would be correct.

The step-by-step tutorial looks straight forward and I haven't seen anyone else come across this problem so I'm a bit perplexed what I've done wrong.

A: 

I'm not sure what your actual question is.

You've requested the root page, \, but have only defined a URL for \hello\, so obviously Django can't find what you've requested. If you want your hello view to match against the site root, do this:

urlpatterns = patterns('',
    (r'^$', hello),
)

I don't understand the question about the from mysite.views import hello. This will work if the parent of mysite is on the Python path.

Daniel Roseman
A: 

you see 404 error, because you don't have the default handler, add to url patterns something like this:

('^$', views.default )

and probably you need to add the web application path to the sys.path variable to be able to 'see' your modules:

import sys
sys.path.append(path_to_site)
dmitko
When I added sys.path.append("..") the mysite.view import error went away. Again, thank you.