ImportError (djangobook chapter 3, part1, python v

2019-08-31 04:47发布

问题:

I am following (http://www.djangobook.com/en/2.0/chapter03/) Djangobook 2, chapter 3, subchapter 'Your First Django-Powered Page: Hello World'.

I get an ImportError, 'no module named views' (check url at the end of the post).

I am using Python 2.7.2

I will go through everything I did step by step:

$ pwd /home/name/Desktop/sc
$ django-admin.py startproject mysite2
$ cd mysite2

$ pwd /home/name/Desktop/sc/mysite2
$ ls -R
.:
manage.py  mysite2  views.py~

./mysite2:
__init__.py   settings.py   urls.py   urls.pyc  wsgi.pyc
__init__.pyc  settings.pyc  urls.py~  wsgi.py

Then I created views.py in ~Desktop/sc/mysite2.

from django.http import HttpResponse

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

Then I went to ~Desktop/sc/mysite2/mysite2. I changed my urls.py to contain the following:

from django.conf.urls.defaults import *
from mysite2.views import hello

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

Then I ran python manage.py runserver in ´/home/name/Desktop/sc/mysite2. When I pastehttp://127.0.0.1:8000/` in my browser I get the following screen:

http://tinypic.com/r/23th72v/5

回答1:

Update: fully working with Django 1.4.0-final

Overall, I'd recommend to re-do the startproject step and start over from scratch; I will summarize the steps you need to take:

1) Create a new Django project

$ django-admin.py startproject helloWorldProject

This creates a new folder helloWorldProject containing some stub files.

2) Create a new file views.py _INSIDE_ your helloWorldProject/helloWorldProject folder. It should contain the following code:

from django.http import HttpResponse

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

3) Update the urls.py file (within the same folder as views.py):

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',
  url(r'^$', 'helloWorldProject.views.hello', name='hello'),
)

4) Run your server using the known command:

$ python manage.py runserver

This should give you some output similar to this

Validating models...

0 errors found
Django version 1.4, using settings 'helloWorldProject.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

5) Check if your "Hello World" view works -- point your browser to http://127.0.0.1:8000/

NOTE: If you define an URL pattern like ('^hello/$', 'helloWorldProject.views.hello', name='hello'),, you have to point your browser to http://127.0.0.1:8000/hello/ as otherwise you will get an HTTP 404 error message...

Hope that helps :)



回答2:

Django book describes old django version. Quote from Chapter 2

Official releases have a version number, such as 1.0.3 or 1.1, and the latest one is always available at http://www.djangoproject.com/download/.

In the newest 1.4 project layout was updated, so be careful to use examples from that book. I advise you to try tutorial from official documentation at first.

I assume you should create views.py in ~Desktop/sc/mysite2/mysite2 near urls.py