2010-09-03 79 views
1

我使用Ubuntu 10,蟒蛇2.6.5这是Django教程或包问题的问题,还是我?

我下面这个教程:http://www.djangobook.com/en/2.0/chapter02
我使用剪切和粘贴遵循所有的步骤。 以下的目录结构被自动创建:

[email protected]:~/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的产生这样的错误:无法导入 'mysite.views'

views.py
from django.http import HttpResponse 

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


[email protected]:~/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. 

其中导致:

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. 

为什么在主目录中的view.py包含以下内容?

from mysite.views import hello

没有子目录'views'。虽然我熟悉使用包,但我从来没有创建自己的需求,所以我有点困惑。我会认为from views import hello是正确的。

一步一步的教程看起来很简单,我还没有看到任何人遇到这个问题,所以我有点困惑,我做错了什么。

+0

可能完全不正确,但应该'r'^ hello/$''是'r'^/hello/$''? – dbr 2010-09-03 13:41:04

+1

@dbr:不,语法是正确的。 – 2010-09-03 13:42:50

+0

本教程告诉你去'http://127.0.0.1:8000/hello/' – 2010-09-03 13:55:25

回答

1

你看到404错误,因为你不具备默认的处理程序,添加到URL模式是这样的:

('^$', views.default) 

,可能需要将Web应用程序路径添加到sys.path中的变量能够'看到'你的模块:

import sys 
sys.path.append(path_to_site) 
+0

当我添加sys.path.append(“..”)mysite.view导入错误消失。再次谢谢你。 – Jarek 2010-09-03 14:20:12

1

我不确定你的实际问题是什么。

您已请求根页面\,但只定义了一个URL为\hello\,所以显然Django无法找到您请求的内容。如果你希望你的hello视图来匹配站点根目录,这样做:

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

我不理解的from mysite.views import hello的问题。如果mysite的父节点位于Python路径上,这将起作用。