关键字arg'之后的Django'非关键字arg,

时间:2013-02-26 02:30:03

标签: django

我正在设计一个学生数据库,我收到此错误'str'对象不可调用。 我找不到错误的位置,所以我会粘贴我的小应用程序。谢谢。

 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) ...

我的models.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

我的views.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'),
  )

1 个答案:

答案 0 :(得分: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'对象不可调用。