python - 'str' 对象不可调用,如何处理?

标签 python django

当我运行我的 Python Django 应用程序时,我收到一个错误:

'str' object is not callable

我已经尝试过这里的解决方案:TypeError: 'str' object is not callable (Python) ,但它们对我不起作用。我正在尝试运行 Django 图书示例:

view.py:

# Create your views here.
from django.http import HttpResponse
import datetime

def current_time(request):
    now = datetime.datetime.now()
    html = "<html><head></head><body>%s</body></html>" % str(now)
    return HttpResponse(html)

def hello(request,name):
    return HttpResponse("Hello django")

def what(request):
    return HttpResponse("what's the problem django?")

urls.py:

from django.conf.urls import patterns, include, url
from hello_django.views import current_time,hello,what


urlpatterns = patterns('',
    url(r'^time/$','current_time'),
    url(r'^what/$','what'),
    url(r'^hello/([a-zA-Z0-9]+)','hello'),
)

这是我正在尝试的 URL:http://127.0.0.1:8000/what/

堆栈跟踪:

TypeError at /what/
'str' object is not callable
Request Method: GET
Request URL:    http://127.0.0.1:8000/what/
Django Version: 1.5.1
Exception Type: TypeError
Exception Value:    
'str' object is not callable
Exception Location: C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response, line 115
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.4
Python Path:    
['D:\\Developer Center\\PyCharm\\helloDjango',
 'C:\\Python27\\lib\\site-packages\\pip-1.3.1-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.4-py2.7-win32.egg',
 'D:\\Developer Center\\PyCharm\\helloDjango',
 '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',
 'C:\\Python27\\lib\\site-packages\\PIL']
Server time:    Tue, 7 Jan 2014 11:44:30 +0330
Traceback Switch to copy-and-paste view

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
                        response = callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars

最佳答案

您需要将实际的 View 提供给url() :

urlpatterns = patterns('',
    url(r'^time/$', current_time),
    url(r'^what/$', what),
    url(r'^hello/([a-zA-Z0-9]+)', hello),
)

请注意,我删除了 what 周围的引号和其他 View 函数。

您仍然可以在 url() 中使用字符串配置,但是你需要使用 <modulename>.<viewname>语法或在 patterns() 的第一个参数中命名模块(字符串),然后您也不需要导入函数:

urlpatterns = patterns('',
    url(r'^time/$', 'hello_django.views.current_time'),
    url(r'^what/$', 'hello_django.views.what'),
    url(r'^hello/([a-zA-Z0-9]+)', 'hello_django.views.hello'),
)

urlpatterns = patterns('hello_django.views',
    url(r'^time/$', 'current_time'),
    url(r'^what/$', 'what'),
    url(r'^hello/([a-zA-Z0-9]+)', 'hello'),
)

参见 detailed URL dispatcher documentation .

关于python - 'str' 对象不可调用,如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20966770/

相关文章:

python - 为什么 python 在导入 A.B.C 时将 'A' 放入全局命名空间

python - 设计一个算法,找出书中最常用的单词

python - 汇总 Pandas 时间戳

python - DefaultRouter 类没有为 python 中的所有应用程序创建 API Root View

python - 从Django中的views.py重定向到另一个url时发生NoReverseMatch错误

Python HTTP 异常处理

python - 如何在不破坏的情况下合并两个字典

python - 单个 View 中的多个模型(django、python)

Python 的 CSV 模块在 Django View 中的行为不同

python - 在 RedHat 上部署 Django 项目