1

我正在设计一个学生数据库,我收到了这个错误'str' object is not callable。我找不到错误的位置,所以我将粘贴我的小应用程序。谢谢。

 TypeError at /school/
 'str' object is not callableRequest Method: GET 
 Request URL: http://tafe.pythonanywhere.com/school/ 
 Django Version: 1.3.5 
 Exception Type: TypeError 
 Exception Value: 'str' object is not callable 
 Traceback Switch to copy-and-paste view
 /usr/local/lib/python2.7/site-packages/django/core/handlers/base.py in get_response 
 1.                    for middleware_method in self._view_middleware:1.                        response = middleware_method(request, callback, callback_args, callback_kwargs)1.                        if response:1.                            break1.1.                if response is None:1.                    try:
  111.                        response = callback(request, *callback_args, **callback_kwargs) ...

我的模型.py

  from django.db import models
  from django.contrib import admin
  class Student(models.Model):
          first_name = models.CharField(max_length=30)
          last_name = models.CharField(max_length=30)
      age = models.BigIntegerField()
      body = models.TextField()

      def __unicode__(self):
          return self.first_name

我的观点.py

  from mysite.school.models import student
  from django.shortcut import render_to_response
  from django.http import HttpResponse

  def index(request):
          students = Student.objects.all()
          render_to_response('index.html',{'students':students})

我的 index.html

Student Database
{% if students %}
<ul>
    {% for student in students %}
    <li>{{ student }}</li>
{% endfor %}
</ul>
{% endif %}

我的网址

  from django.conf.urls import patterns,include , url 
  from django.contrib import admin
  from mysite.school.views import index
  admin.autodiscover()
  urlpatterns = patterns ('',
url(r'^$','index'),
  )
4

1 回答 1

2

问题在于您的 urls.py。它应该是:

from django.conf.urls import patterns, include, url 
from django.contrib import admin
from mysite.school.views import index

admin.autodiscover()


urlpatterns = patterns ('',
    url(r'^$', index),
)

为了节省一些打字,我会这样做:

urlpatterns = patterns ('mysite.school.views',
    url(r'^$', 'index'),
)

这就是我认为您首先尝试做的事情。因此错误'str'对象是不可调用的。

于 2013-02-26T02:54:00.670 回答