Django NameError

时间:2013-03-26 13:18:42

标签: python django model-view-controller error-handling

我设置了python和django,并且只是想构建一个简单的应用程序'mysite'

我在根文件夹中创建了views.py,而我的urls.py在root / mysite /中 当我调用/ hello时它会给我以下错误。我该怎么做才能解决它?

NameError at /hello
name 'hello' is not defined
Request Method: GET
Request URL:    ~~:8000/hello
Django Version: 1.5
Exception Type: NameError
Exception Value:    
name 'hello' is not defined
Exception Location: c:\Python27\Django-1.5\mysite\mysite\urls.py in <module>, line 17
Python Executable:  c:\Python27\python.exe
Python Version: 2.7.2
Python Path:    
['c:\\Python27\\Django-1.5\\mysite',
 'c:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg',
 'c:\\Python27\\lib\\site-packages\\pymysql-0.3-py2.7.egg',
 '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']
Server time:    Tue, 26 Mar 2013 18:40:41 +0530
Traceback Switch to copy-and-paste view

c:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
                    response = middleware_method(request)
 ...
▶ Local vars
c:\Python27\lib\site-packages\django\middleware\common.py in process_request
            if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
 ...
▶ Local vars
c:\Python27\lib\site-packages\django\core\urlresolvers.py in is_valid_path
        resolve(path, urlconf)
 ...
▶ Local vars
c:\Python27\lib\site-packages\django\core\urlresolvers.py in resolve
    return get_resolver(urlconf).resolve(path)
 ...
▶ Local vars
c:\Python27\lib\site-packages\django\core\urlresolvers.py in resolve
            for pattern in self.url_patterns:
 ...
▶ Local vars
c:\Python27\lib\site-packages\django\core\urlresolvers.py in url_patterns
        patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
 ...
▶ Local vars
c:\Python27\lib\site-packages\django\core\urlresolvers.py in urlconf_module
            self._urlconf_module = import_module(self.urlconf_name)
 ...
▶ Local vars
c:\Python27\lib\site-packages\django\utils\importlib.py in import_module
    __import__(name)
 ...
▶ Local vars
c:\Python27\Django-1.5\mysite\mysite\urls.py in <module>
                       ('^hello/$', hello),
 ...

2 个答案:

答案 0 :(得分:3)

更改urls.py中的以下行

('^hello/$', hello),

('^hello/$', 'views.hello'),

或者如果您使用的是前缀,请执行

('^hello/$', 'hello'),

或做

from views import hello

答案 1 :(得分:0)

c:\Python27\Django-1.5\mysite\mysite\urls.py in <module>
                       ('^hello/$', hello),

您尚未定义hello,我相信您可以导入hte函数或将其作为字符串引用

from mysite.myapp.views import hello

'mysite.myapp.views.hello'