0

我设置了 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),
 ...
4

2 回答 2

3

在 urls.py 中更改以下行

('^hello/$', hello),

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

或者如果你使用前缀然后做

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

或者做

from views import hello
于 2013-03-26T13:22:27.090 回答
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'
于 2013-03-26T13:21:36.203 回答