我正在使用Ubuntu 10,python 2.6.5

我正在关注本教程:http://www.djangobook.com/en/2.0/chapter02
我使用剪切和粘贴遵循了所有步骤。
自动创建以下目录结构:

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产生此错误:无法导入“ 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.


结果是:

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包含以下内容?

从mysite.views导入你好

没有子目录“视图”。尽管我熟悉使用软件包,但是我从来不需要创建自己的软件包,因此有点困惑。我本以为from views import hello是正确的。

分步教程看起来直截了当,而且我还没有看到其他人遇到过此问题,因此我对自己做错的事情感到困惑。

最佳答案

您会看到404错误,因为您没有默认处理程序,因此请向url模式添加以下内容:

('^$', views.default )


可能您需要将Web应用程序路径添加到sys.path变量中才能“查看”模块:

import sys
sys.path.append(path_to_site)

关于python - 这是Django教程问题还是软件包问题,还是我?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3636280/

10-14 10:01
查看更多